Using $_POST to get select option value from HTML - php

I use select as below:
<select name="taskOption">
<option>First</option>
<option>Second</option>
<option>Third</option>
</select>
How do I get the value from the select option and store it into a variable for future use, in PHP?

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>

You can access values in the $_POST array by their key. $_POST is an associative array, so to access taskOption you would use $_POST['taskOption'];.
Make sure to check if it exists in the $_POST array before proceeding though.
<form method="post" action="process.php">
<select name="taskOption">
<option value="first">First</option>
<option value="second">Second</option>
<option value="third">Third</option>
</select>
<input type="submit" value="Submit the form"/>
</form>
process.php
<?php
$option = isset($_POST['taskOption']) ? $_POST['taskOption'] : false;
if ($option) {
echo htmlentities($_POST['taskOption'], ENT_QUOTES, "UTF-8");
} else {
echo "task option is required";
exit;
}

You can do it like this, too:
<?php
if(isset($_POST['select1'])){
$select1 = $_POST['select1'];
switch ($select1) {
case 'value1':
echo 'this is value1<br/>';
break;
case 'value2':
echo 'value2<br/>';
break;
default:
# code...
break;
}
}
?>
<form action="" method="post">
<select name="select1">
<option value="value1">Value 1</option>
<option value="value2">Value 2</option>
</select>
<input type="submit" name="submit" value="Go"/>
</form>
for php8+ versions, you can use match expression:
$select = $_POST['select1'] ?? '';
$result = match ($select) {
'value1' => 'this is value1',
'value2' => 'this is value2',
default => 'unknown value',
};
echo $result;

<select name="taskOption">
<option value="first">First</option>
<option value="second">Second</option>
<option value="third">Third</option>
</select>
$var = $_POST['taskOption'];

Depends on if the form that the select is contained in has the method set to "get" or "post".
If <form method="get"> then the value of the select will be located in the super global array $_GET['taskOption'].
If <form method="post"> then the value of the select will be located in the super global array $_POST['taskOption'].
To store it into a variable you would:
$option = $_POST['taskOption']
A good place for more information would be the PHP manual: http://php.net/manual/en/tutorial.forms.php

Like this:
<?php
$option = $_POST['taskOption'];
?>
The index of the $_POST array is always based on the value of the name attribute of any HTML input.

<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
try this
<?php
if(isset($_POST['button_name'])){
$var = $_POST['taskOption']
if($var == "1"){
echo"your data here";
}
}?>

-- html file --
<select name='city[]'>
<option name='Kabul' value="Kabul" > Kabul </option>
<option name='Herat' value='Herat' selected="selected"> Herat </option>
<option name='Mazar' value='Mazar'>Mazar </option>
</select>
-- php file --
$city = (isset($_POST['city']) ? $_POST['city']: null);
print("city is: ".$city[0]);

Related

Get value of drop down menu and save as variable in PHP

I want to get the selected value of a drop down menu and save it as a variable. I tried the following as documented in another post, but it seems not working: echo $selected = $_POST['<?php $as->my_name(); ?>'];
Drop down:
<select name="<?php $as->my_name(); ?>">
<option value="">Select this</option>
<option value="91"<?php $mb->state('91'); ?>>91</option>
<option value="90"<?php $mb->state('90'); ?>>90</option>
<option value="89"<?php $mb->state('89'); ?>>89</option>
<option value="88"<?php $mb->state('88'); ?>>88</option>
<option value="87"<?php $mb->state('87'); ?>>87</option>
<option value="86"<?php $mb->state('86'); ?>>86</option>
<option value="85"<?php $mb->state('85'); ?>>85</option>
<option value="84"<?php $mb->state('84'); ?>>84</option>
<option value="83"<?php $mb->state('83'); ?>>83</option>
<option value="82"<?php $mb->state('82'); ?>>82</option>
</select>
Post the form:
You have to put the select option within form tags
<form method="post">
<select name="example">
<option></option>
</select>
<input type="submit" />
</form>
Get the posted value:
To get the value of this example you can use:
<?php
$selected = $_POST['example'];
echo $selected;
?>
To get the value in your case:
<select name="<?php echo $as->my_name(); ?>">
<option value="test">test</option>
</select>
<?php
$selected = $_POST[$as->my_name()];
echo $selected;
?>

passing values to sql through check boxes and dropdowns

How can i pass Drop down values to sql database and also the check box for example if a user selects English and maths than the value inserted in to the database would be 1 or else the value would be 0
<form>
<p id="p1">Select Your Year</p>
<select id="year_sel">
<option value="blank"></option>
<option id="primary" value="primary">Primary</option>
<option value="1">Year one</option>
<option value="2">Year two</option>
<option value="3">Year Three</option>
</select>
<input type="checkbox" name="Math" value="Math">Math<br>
<input type="checkbox" name="English" value="English">English<br>
<input type="checkbox" name="HealthScience" value="HealthScience">Health Science<br>
<input class="sub_reg" type="submit" value="Register Subjects" />
</form>
this is how my database looks like
First, your <select id="year_sel"> needs a name attribute to post ->
<select id="year_sel" name="year_sel" >
Since you are using <form> the default is get, so you would get the selected value in $_GET
$year_sel = $_GET['year_sel'];
If you changed it to
<form method="post">
then you would get it in $_POST
$year_sel = $_POST['year_sel']
Second, checkboxes are only posted if checked, so you can use isset() to set a value using a ternary -
$math = isset($_GET['Math']) ? 1 : 0;
$english = isset($_GET['English']) ? 1 : 0;
...[rest of your checkboxes]...
swap $_GET/$_POST like the select
$math = isset($_POST['Math']) ? 1 : 0;
$english = isset($_POST['English']) ? 1 : 0;
<select id="year_sel" name="year_sel">
<option value="primary">Primary</option>
<option value="1">Year one</option>
<option value="2">Year two</option>
<option value="3">Year Three</option>
</select>
on your form action something.php file use this to get the select value
$language = $_POST["year_sel"]; // chose whetever your form method
establish the database connection please do refer some tutorials (if you don't know )
write the mysqli insert command like
$SQL = "INSERT INTO yourtable (language) VALUES ('$language')";
mysqli_real_escape_string($sql);
$result = mysqli_query($sql) or die (mysqli_error());
now you can insert this value in your mysql or any database table
this is not tested
<form>
<p id="p1">Select Your Year</p>
<select id="year_sel" name="year_sel">
<option value="blank"></option>
<option id="primary" value="primary">Primary</option>
<option value="1">Year one</option>
<option value="2">Year two</option>
<option value="3">Year Three</option>
</select>
<?php
$variable = $_POST["year_sel"];
?>
Should mention the HTML ELEMENT name to get the value.

How to get 'select' value and put into 'input' value?

I have a for with a select dropdown option. I want to get the value that is selected and put it into a hidden input value. I'm trying this at the minute -
echo '<form method="post" action="cart_update.php">';
echo"<select name='qty'>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10>10</option>
</select>";
echo '<input type="hidden" name="product_qty" value="'.$_GET['qty'].'" />';
echo"</form>;
When I submit this to cart-update.php it tells me that qty is an undefined index on the hidden input line.
Can anyone see the problem?
You will have to use javascript or jQuery. Some sample jQuery code:
$('select').change(function() {
var val1 = $('select').val();
$('your selector').val(val1);
});
Consider this instead:
$arr = array(1,2,3,4,5,6,7,8,9,10);
foreach($arr as $n){
$selected = $n == $_POST['qty'] ? 'selected' : '';
echo "<option value='$n' $selected>$n</option>";
}
Based on #Undefined_variable answer:
/***************************/
jQuery( document ).ready(function( $ ) {
/* selecy key from list , insert into field */
$('#select_start_key').change(function(){
var val1 = $('#select_start_key').val();
$('#start_key').val(val1);
/*reset the select to none*/
$('#select_start_key').val("");
});
});
View example :
https://jsfiddle.net/alinrzv/aj619Lbb/
A few problems:
1. Your form is in POST method, and you are trying to get the value in "Get" method ($_GET)
2. The value of $_GET will be available only after submitting the form.
You should use javascript to get the value. something like that:
<form method="post" action="cart_update.php">
<select name='qty' onChange='document.getElementById("product_qty").value = this.value'>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10>10</option>
</select>
<input type="hidden" id="product_qty" name="product_qty" value="1" />

How to select the dropdownlist value and validate it

I have created the dropdown list in my index page i want to select the one value from that list and validate it if not selected any value the code for that is as follows:
<form action="" method="post">
<select value="state" name="state">
<option selected="">---please enter---</option>
<option value="1">andhra</option>
<option value="2">thamil</option>
<option value="3">kerela</option>
</select>
</form>
php code for the above file is as follows:
<?php
if(!empty($_POST['state'])) {
$state = $_POST['state'];
}
else {
echo "required";
}
?>
I dont want to be select the first option in selection list please enter to be selected but the code which I have used is taking that value also I want relevant code how to validate that list?
I prefer adding a disabled attribute to the placeholder option, that way, a user has to choose something if they click the drop-down:
<select value="state" name="state">
<option selected disabled>---please enter---</option>
<option value="1">andhra</option>
<option value="2">thamil</option>
<option value="3">kerela</option>
</select>
Quick demo: http://jsfiddle.net/hxxJZ/
something like this should to the trick:
<form action="" method="post">
<select name="state">
<option value="0" selected>---please enter---</option>
<option value="1">andhra</option>
<option value="2">thamil</option>
<option value="3">kerela</option>
</select>
</form>
php
<?php
if( 0 != $_POST['state'] ) {
$state = $_POST['state'];
}
else {
echo "required";
}
?>
your HTML is not right,
try edit this line
<option selected=""...
to this one
<option selected value="">---
select elements do not have an (HTML) value attribute, so remove value="state".
The selected attribute should be selected="selected" (or just SELECTED).

how to retain selected values in Select field after form submission?

i have a main page that i used in uploading tickets to db, i have Select field that i want to retain the value that the user selected prior to submitting the form but its not happening...
here is my code for select field:
<select name="XiD" id="XiD">
<option value="Blank" selected="selected">Please Select...</option>
<option value="AAA">AAA</option>
<option value="BBB">BBB</option>
<option value="CCC">CCC</option>
<option value="DDD">DDD</option>
<option value="EEE">EEE</option>
<option value="FFF">FFF</option>
</select>
just to add, this Xid is being posted prior to submitting
$XiD = $_POST['XiD'];
and i try to use this script:
UPDATE - changed from $_GET to $_POST but still not working...
<script type="text/javascript">
document.getElementById('XiD').value = "<?php echo $_POST['XiD'];?>";
</script>
my jquery version im using: jquery-ui-1.10.2 and jquery-1.9.1
Use jQuery like this.
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('select#XiD').val('<?php echo $_POST['XiD'];?>');
});
</script>
You are using PHP to generate HTML. There's absolutely no need to use a third language to dictate how that HTML should be!
<select name="XiD" id="XiD">
<option value="Blank"<?=$XiD=='Blank' ? ' selected="selected"' : ''?>>Please Select...</option>
<option value="AAA"<?=$XiD=='AAA' ? ' selected="selected"' : ''?>>AAA</option>
...
</select>
Or, even better, simplify your code with an array and avoid writing duplicate code that's hard to maintain:
<?php
$options = array(
'Blank' => 'Please Select...',
'AAA' => 'AAA',
'BBB' => 'BBB',
'CCC' => 'CCC',
'DDD' => 'DDD',
'EEE' => 'EEE',
'FFF' => 'FFF',
);
<select name="XiD" id="XiD">
<?php foreach(options as $k => $v){ ?>
<option value="Blank"<?php echo $XiD==$k ? ' selected="selected"' : ''?>>Please Select...</option>
<? } ?>
</select>
According to your problem, it seems like you are using POST method (because you are getting value to $XiD = $_POST['XiD'];) and using GET to retrieve value.
So, use document.getElementById('XiD').value = "<?php echo $_POST['XiD'];?>";
instead of
document.getElementById('XiD').value = "<?php echo $_GET['XiD'];?>";
I would create a function to check if the current GET value is the same as the option in the list and in that case, return the needed attribute to make it selected by default, which is selected="selected".
This should work:
<?php
function isSelected($value){
if($value == $_GET['XiD']){
return 'selected="selected"';
}else{
return '';
}
}
?>
<select name="XiD" id="XiD">
<option value="Blank" <? echo isSelected('Blank');?>>Please Select...</option>
<option value="AAA" <? echo isSelected('AAA');?>>AAA</option>
<option value="BBB" <? echo isSelected('BBB');?>>BBB</option>
<option value="CCC" <? echo isSelected('CCC');?>>CCC</option>
<option value="DDD" <? echo isSelected('DDD');?>>DDD</option>
<option value="EEE" <? echo isSelected('EEE');?>>EEE</option>
<option value="FFF" <? echo isSelected('FFF');?>>FFF</option>
</select>
After trying al this "solves" nothing work. Did some research on w3school before and remember there was explanation of keeping values about radio. But it also works for Select option. See here an example. Just try it out and play with it.
<?php
$example = $_POST["example"];
?>
<form method="post">
<select name="example">
<option <?php if (isset($example) && $example=="a") echo "selected";?>>a</option>
<option <?php if (isset($example) && $example=="b") echo "selected";?>>b</option>
<option <?php if (isset($example) && $example=="c") echo "selected";?>>c</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>

Categories