Using $_Post to Get Select Option Value from Html

Using $_POST to get select option value from HTML

Use this way:

$selectOption = $_POST['taskOption'];

But it is always better to give values to your <option> tags.

<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>

Using POST to get select option value from HTML form

Your <select> for a multiple selectable dropdown needs to be an array, defined by name="NameThatMakesSense[]"

<select name="NameThatMakesSense[]" multiple>

This will then return you an field called $_POST['NameThatMakesSense'] which itself is an array containing 1 or more values indicating which items in the dropdown were selected. From the value="" attribute of the <option> tag

Then your will need to preprocess the NameThatMakesSense array to pick up which values were actually selected form the multi select dropdown.

$esea = 0;
$esea = 0;
$esea = 0;
foreach ( $_POST['NameThatMakesSense'] as $value ) {
if ( $value == 'esea' ) { $esea = 1; }
if ( $value == 'faceit' ) { $faceit= 1; }
if ( $value == 'matchmaking' ) { $matchmaking= 1; }

$sql = "INSERT INTO users
( profilename, profileurl, avatar, region,
age, esea, faceit, matchmaking, textarea1 )


how to get the selected option in value in php

I have used a form to submit what the user has selected. When the user submits the form the selected value will be posted and taken into the variable $entry_order_type. I think this is what you are expecting. Hope it helps. :)

$entry_order_type ="Select order type from the list and press the submit button";
$entry_order_type = $_POST['filter_order_type']; // Storing Selected Value In Variable
<form action="#" method="post">
<div class="form-group">
<label class="control-label" for="input-order-type"><?php echo $entry_order_type; ?></label>

<select name="filter_order_type" id="input-order-type" class="form-control">

<option value=""></option>

<option value="FB Orders" <?php if($entry_order_type=="FB Orders"){echo "selected";} ?> >FB Orders</option>
<option value="Direct Orders" <?php if($entry_order_type=="Direct Orders"){echo "selected";} ?> >Direct Orders</option>
<option value="Khathija's Orders" <?php if($entry_order_type=="Khathija's Orders"){echo "selected";} ?> >Khathija's Orders</option>
<option value="Ameer's Orders" <?php if($entry_order_type=="Ameer's Orders"){echo "selected";} ?> >Ameer's Orders</option>
<option value="Saheed's Orders" <?php if($entry_order_type=="Saheed's Orders"){echo "selected";} ?> >Saheed's Orders</option>
<option value= "Nada's Orders" <?php if($entry_order_type=="Nada's Orders"){echo "selected";} ?> >Nada's Orders</option>
<option value="Henath's Orders" <?php if($entry_order_type=="Henath's Orders"){echo "selected";} ?> >Henath's Orders</option>
<option value="Noha's Orders" <?php if($entry_order_type=="Noha's Orders"){echo "selected";} ?> >Noha's Orders</option>
<input type="submit" name="submit" value="Get Selected Values" />


POST or GET selected value from select option before submit (same page)

You need to work with AJAX for this. AJAX allows the sending of data from the front(JS) to the back(PHP). JQuery makes this easy by providing you with the .ajax() method. This is how you need to do this:

Note: You will need to use jQuery to do it this way

First, re-arrange your HTML in this manner, notice the select ids:

<form name="form" method="POST">
<div class="form-group">
<div><label>Floor Name</label></div>
<select class="form-control" id="floor_select" name="existfloorname">
$floors = $conn->query("select * from floors");
while ($row = $floors->fetch_assoc()){ ?>
<option value="<?= $row['floorid']; ?>"><?= $row['floorname']; ?></option>
<?php } ?>

<div class="form-group">
<div><label>Room Name</label></div>
<select class="form-control" id="room_select" name="existroomname">
<input type="submit" name="delete" value="Delete">

Second, create your jQuery ajax call:

    $(document).on("change", 'select#floor_select', function(e) {
var floor_id = $(this).val();

type: "POST",
data: {floor_id: floor_id},
url: 'get_room_list.php',
dataType: 'json',
success: function(json) {
var $el = $("select#room_select");
$el.empty(); // remove old options
$el.append("<option>Please Select</option>");
//iterate through your results and add to your dropdown
$.each(json, function(k, v) {
$el.append("<option value='" + + "'>" + v.roomname + "</option>");


Third, create the PHP script that the AJAX call above is looking for.
Call it get_room_list.php:

$result = $conn->query("select * from rooms where floorid = '" . GETSELECTEDFLOORID . "'");
while ($row = $result->fetch_assoc()) {
$results[] = $row;
header('Content-Type: application/json');
echo json_encode($results);

Once all the above is done, you should be able to obtain the information you need. Every time the "Floor Name" select is changed, JS will send a POST request (via AJAX) to PHP and PHP will return the "rooms" result in JSON format, which you then iterate to create your dropdown.

Hope this all makes sense.

Good luck.

PHP: HTML: send HTML select option attribute in POST

<form name="add" method="post">
<select name="age">
<option value="1_sre">23</option>
<option value="2_sam">24</option>
<option value="5_john">25</option>
<input type="submit" name="submit"/>

You will have the selected value in $_POST['age'], e.g. 1_sre. Then you will be able to split the value and get the 'stud_name'.

$stud = explode("_",$_POST['age']);
$stud_id = $stud[0];
$stud_name = $stud[1];

Set option as selected with php

Just add the selected="selected" attribute to the option tag that you received under $_POST['s'].

$selectedOption = '';
if(isset($_POST['btSubmit'])) {
$selectedOption = $_POST['s']; // Value of selected option … but how to use it below?

function injectSelectedAttribute($selectedOption, $option_value){
return strtolower($selectedOption) === strtolower($option_value) ? 'selected="selected"' : '';
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
<select name="s">
<option value="" disabled>-- choose one --</option>
<option value="a" <?php echo injectSelectedAttribute($selectedOption, 'a'); ?>>choose a</option>
<option value="b" <?php echo injectSelectedAttribute($selectedOption, 'b'); ?>>choose b</option>
<input type="submit" name="btSubmit">

You can further improve this to echo HTML of the options via looping on a PHP array of select options.

Related Topics

Leave a reply