How to post value data from <option> in php?

Member

by dedrick , in category: PHP General , 6 months ago

How to post value data from <option> in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by domenico , a month ago

@dedrick 

To post the value data from an tag in PHP, you will need to use a form element and submit the selected value to a PHP file for processing.


Here's an example of how you can do this:

  1. Create a form in your HTML file that contains the tag:
1
2
3
4
5
6
7
8
<form action="process.php" method="post">
    <select name="option">
        <option value="value1">Option 1</option>
        <option value="value2">Option 2</option>
        <option value="value3">Option 3</option>
    </select>
    <input type="submit" value="Submit">
</form>


  1. Create a PHP file (e.g. process.php) that will receive the selected value:
1
2
3
4
5
6
<?php
if(isset($_POST['option'])){
    $selected_option = $_POST['option'];
    echo "Selected option: " . $selected_option;
}
?>


When the form is submitted, the selected value from the tag will be sent to the process.php file using the POST method. The selected value can then be accessed in the process.php file using the $_POST superglobal.