Hi everyone,
Can you please let me know how I can assign the selected value from a dropdown list into a $variable and print or echo it in the screen

For Example if I have a dropdown list of years like:

<?php
$years = array();
for ($i = 1900; $i < 2026; $i++)
{
  $years[] = $i;
} 
echo "<select name='Year'>";
foreach($years as $option){
	echo "<option value='{$option}'>{$option}</option>";	
}
echo "</select>";
?>

Now I want to save the selected value in a variable to do any further process?
Thanks for your time, in advance.

Member Avatar for diafol
<?php
$years = array();
echo "<select name='Year'>";
for ($i = 1900; $i < 2026; $i++){
	echo "<option value='$i'>$i</option>";	
}
echo "</select>";
?>

OK that's your form

Depending on which method you use ('get' or 'post'), you pick up the variable like this:

$year = $_POST['Year'];

OR

$year = $_GET['Year'];

This will probably throw an error if you include this on the same page as the form. Forms should be sent to form handling files to do the processing.

If you want to display the last known selection after sending the form:

//assume you've placed the selection into a variable called $year:

echo "<select name='Year'>";
for ($i = 1900; $i < 2026; $i++){
   $sel = (isset($year) && $year == $i) ? ' selected="selected"' : '';
   echo "<option value='$i'$sel>$i</option>";	
}
echo "</select>";

Not tested.

Hi ardav,
Thanks for your reply as you said I want to retrieve the selected value without calling any external php actions.
I tried your code but it didn't, go through and I wasn't able to assign the selected value into a variable.

//assume you've placed the selection into a variable called $year:

echo "<select name='Year'>";
for ($i = 1900; $i < 2026; $i++){
   $sel = (isset($year) && $year == $i) ? ' selected="selected"' : '';
   echo "<option value='$i'$sel>$i</option>";	
}
echo "</select>";

Thanks anyway,

You need javascript or jQuery. Everything to be requested on the user side has to be handle by javascript, now, if you just want to refresh the page and then retrieve the information you need to use ardav solution as well, for example with a GET method the form attribute should look like this:

<form name="nameOfForm" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$years = array();
for ($i = 1900; $i < 2026; $i++)
{
  $years[] = $i;
} 
echo "<select name='Year'>";
foreach($years as $option){
	echo "<option value='{$option}'>{$option}</option>";	
}
echo "</select>";
?>
<input type="submit" name="submit" value="Save Year" />
</form>
<?php
if($_GET){
   echo 'The year selected is '.$_GET['Year'];
}

// also you can retrieve all the elements of the form creating dynamic variables like this

if($_GET){
  foreach($_GET as $k=>$v){

     ${$k}=$v;

  }
}
?>

In the action of the for I'm using $_SERVER which is the same page the form is, when form is submitted then the form is processed in the same page.

If you retrieving all the elements as an array the variables will be named after the field names example:

<select name="Year"> will be the variable $Year and <input name="submit"> will be variable $submit.

Hope this help.

commented: Perfect Reply +3

Perfect!

This is exactly what I was looking for. I really appreciate

hi I want to select value from drop down list and selected value should be print in another Textbox without using submit button.

Member Avatar for diafol

hi I want to select value from drop down list and selected value should be print in another Textbox without using submit button.

That's javascript - this is the PHP forum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.