We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to Display Selected Value From Dropdown List

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.

4
Contributors
6
Replies
8 Months
Discussion Span
11 Months Ago
Last Updated
12
Views
Question
Answered
Behseini
Junior Poster
148 posts since Feb 2011
Reputation Points: 8
Solved Threads: 2
Skill Endorsements: 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.

diafol
Keep Smiling
Moderator
10,826 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,532
Skill Endorsements: 61

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,

Behseini
Junior Poster
148 posts since Feb 2011
Reputation Points: 8
Solved Threads: 2
Skill Endorsements: 0

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.

raphie
Light Poster
44 posts since Jun 2009
Reputation Points: 13
Solved Threads: 4
Skill Endorsements: 0

Perfect!

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

Behseini
Junior Poster
148 posts since Feb 2011
Reputation Points: 8
Solved Threads: 2
Skill Endorsements: 0
Question Answered as of 1 Year Ago by diafol and raphie

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

sivamca
Newbie Poster
1 post since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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.

diafol
Keep Smiling
Moderator
10,826 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,532
Skill Endorsements: 61

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.1036 seconds using 2.68MB