PHP Get Dropdown Value and Text

PHP get dropdown value and text

$animals = array('--Select Animal--', 'Cat', 'Dog', 'Cow');
$selected_key = $_POST['animal'];
$selected_val = $animals[$_POST['animal']];

Use your $animals list to generate your dropdown list; you now can get the key & the value of that key.

Get selected text from dropdown in PHP

You can use a hidden field, and with JavaScript and jQuery you can set the value to this field, when the selected value of your dropdown changes.

<select id="itemType_id" name="cscf[country]" class="input-xlarge">
<option value="malaysia@email.com">Malaysia</option>
<option value="indonesia@email.com">Indonesia</option>
</select>
<input type="hidden" name="country" id="country_hidden">

<script>
$(document).ready(function() {
$("#itemType_id").change(function(){
$("#country_hidden").val(("#itemType_id").find(":selected").text());
});
});
</script>

Then when your page is submitted, you can get the name of the country by using

$_POST["country"]

Get True/False text result from drop down selection

I would use an array which holds the state of the values, then you simply check the value which will determine the state.

<?php
$citys = [
'Richmond' => true,
'Bowling Green' => true,
'Manakin' => false,
'Emporia' => false
];
?>

<select name="City">
<?php foreach ($citys as $city => $avail): ?>
<option value="<?= $city ?>"><?= $city ?></option>
<?php endforeach; ?>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>

<?php
if (isset($_POST['submit'])) {
$selected_val = $_POST['City'] ?? ''; // Storing Selected Value In Variable

if (isset($citys[$selected_val])) {
echo "You have selected:".$selected_val." and this location is ".($citys[$selected_val] ? '' : 'Not').' served';
} else {
// error, city not in array
}
}
?>

PHP get value from Drop down list

Take a look at the field you're POSTing:

<select name="color" id="color">
...
</select>

When accessing the $_POST array, you're not looking for the value that gets posted, you're looking for the field, which is, in this case, color, not red, green or blue.

You've got the right idea, but your if statement is looking for the wrong thing, and using the wrong = (should be == for comparison):

if($_POST['color'] == 'red'){
echo "<span style='color:red;'>Hi ".$name."</span>";
} else if ($_POST['color'] == 'blue'){
echo "<span style='color:blue;'>Hi ".$name."</span>";
} else if ($_POST['color'] == 'green'){
echo "<span style='color:green;'>Hi ".$name."</span>";
}

Fix those errors and your code should work. Also, look into using an IDE, which would show you these errors (especially the = vs == one).

How to get dropdown's value and text both in form.serialize?

serialize() will only retrieve the name and value properties from an element.

To do what you require you can use serialize() as normal, then append the selected option text to it:

var data = $('form').serialize() + '&attendText=' + $('select option:selected').text();
console.log(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><form>  <select name='attend'>    <option value="1" selected="selected">Question</option>    <option value="2">Attending</option>    <option value="3">not-attending</option></select></form>

Get value of selected option from dynamic dropdown using php

I hope I understood correctly that after selecting a city from the second dropdown you also wish that value to appear after the country name? You could perhaps try like this - though no doubt jQuery has a better method of assigning the event handler to the second dropdown than this(?)

The php is in the same file here for testing only.

By adding selected disabled to both dropdowns means the initial value of select or please select cannot be chosen once a selection has been made- just a little enhancement perhaps ;-)

<?php

if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST["country"] ) ){

$cities = array(
'usa' => array('New York', 'Los Angeles', 'California'),
'india' => array('Mumbai', 'New Delhi', 'Bangalore'),
'uk' => array('London', 'Manchester', 'Liverpool')
);

if( isset( $_POST['country'] ) && array_key_exists( $_POST['country'], $cities ) ){

$arr=$cities[ $_POST['country'] ];

echo "
<label>City:</label>
<select id='state' class='state' onchange='dispcity(this.value)'>
<option selected disabled>Please Select";

foreach( $arr as $key => $value ){
printf("<option value='%s'>%s",$value,$value);
}
echo "
</select>

<span id='info'>
{$_POST['country']}
</span>";

}
exit();
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery Populate City Dropdown Based on Country Selected</title>
<script src="//code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select.country").change(function(){
var selectedCountry = $(".country option:selected").val();
if( selectedCountry=='Select' ){
$("#response").html('');
return;
}
$.ajax({
type: "POST",
url: location.href, //"post_request.php",
data: { country : selectedCountry }
}).done(function(data){
$("#response").html(data);
});
});
});
function dispcity( value ){
$("#info").html( $("select.country").val() +', '+ value )
}
</script>
</head>
<body>
<form>
<table>
<tr>
<td>
<label>Country:</label>
<select class="country">
<option selected disabled>Select Country</option>
<option value="usa">United States</option>
<option value="india">India</option>
<option value="uk">United Kingdom</option>
</select>
</td>
<td id="response">
<!--Response will be inserted here-->
</td>
</tr>
</table>
</form>
</body>
</html>

PHP get text or value from dropdown list

You need to put your select inside of a form, and have the form submission point to your next page. Like:

<form action='[yourNextPage.php]' method='post'>
<select name="tanulok">
<?php
while ($row = mysql_fetch_array($result))
{
echo "<option value=".$row['nev'].">".$row['nev']."</option>";
}
?>
</select>
<input type='submit' value='Submit'>
</form>

Then in your next page, you can access the value the user selected through the $_POST array, like:

$_POST['tanulok']


Related Topics



Leave a reply



Submit