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>
</select>
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 )
VALUES (
'{$mysqli->real_escape_string($_POST['profilename'])}',
'{$mysqli->real_escape_string($_POST['profileurl'])}',
'{$mysqli->real_escape_string($_POST['avatar'])}',
'{$mysqli->real_escape_string($_POST['region'])}',
'{$mysqli->real_escape_string($_POST['age'])}',
'{$mysqli->real_escape_string($_POST['ranks'])}',
$esea,
$faceit,
$matchmaking,
'{$mysqli->real_escape_string($_POST['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. :)
<?php
$entry_order_type ="Select order type from the list and press the submit button";
if(isset($_POST['filter_order_type'])){
$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>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</div>
</form>
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">
<option>None</option>
<?php
$floors = $conn->query("select * from floors");
while ($row = $floors->fetch_assoc()){ ?>
<option value="<?= $row['floorid']; ?>"><?= $row['floorname']; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<div><label>Room Name</label></div>
<select class="form-control" id="room_select" name="existroomname">
<option>None</option>
</select>
</div>
<input type="submit" name="delete" value="Delete">
</form>
Second, create your jQuery ajax call:
$(document).on("change", 'select#floor_select', function(e) {
var floor_id = $(this).val();
$.ajax({
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.id + "'>" + v.roomname + "</option>");
});
}
});
});
Third, create the PHP script that the AJAX call above is looking for.
Call it get_room_list.php
:
<?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">
<p>Age:</p>
<select name="age">
<option value="1_sre">23</option>
<option value="2_sam">24</option>
<option value="5_john">25</option>
</select>
<input type="submit" name="submit"/>
</form>
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']
.
<?php
$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>
</select>
<input type="submit" name="btSubmit">
</form>
You can further improve this to echo HTML of the options via looping on a PHP array of select options.
Related Topics
How to Remove All Non Printable Characters in a String
What Are the Disadvantages of Using Persistent Connection in Pdo
Get the Client Ip Address Using PHP
Get Specific Columns Using "With()" Function in Laravel Eloquent
How to Make Pdf File Downloadable in HTML Link
How to Enable Curl in PHP/Xampp
How to Recursively Delete a Directory and Its Entire Contents (Files + Sub Dirs) in PHP
"Connect Failed: Access Denied For User 'Root'@'Localhost' (Using Password: Yes)" from PHP Function
Get PHP to Stop Replacing '.' Characters in $_Get or $_Post Arrays
Declaration of Methods Should Be Compatible With Parent Methods in PHP
Get Url Query String Parameters
How to Loop Through This Array in PHP
How to Extend a Class Using More Than 1 Class in PHP
MySQLi Prepared Statements Error Reporting
Get Name of Caller Function in PHP