0

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.

5
Contributors
7
Replies
59
Views
5 Years
Discussion Span
Last Post by dheerajvermawd
0
<?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.

0

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,

1

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.

Edited by raphie: some typos

Votes + Comments
Perfect Reply
0

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

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.