Please register or login. There are 0 registered and 404 anonymous users currently online. Current bandwidth usage: 326.30 kbit/s November 30 - 02:55pm EST 
Hardware Analysis
Forums Product Prices

  Latest Topics 

More >>


  You Are Here: 
/ Forums / Programming /

  PHP Select drop down box 
 Date Written 
jeremy scharlow May 04, 2013, 02:37pm EDT Report Abuse

I have created a drop down box and when I submit it i want the selected item to remain selected in the drop down box instead of going back to the first option in the drop down box.

below is my code:


$selmonths = array(1=>'January', 'February, 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'

echo "<form method='post' action=' '>";

echo "<select name='months' >";
foreach ($selmonths as $i=>$opt ):
echo "<option value='$i'>$opt</option>";
echo "</select>";

echo "<input type =submit value=Submit>";


Any help would be appreciated.

Want to enjoy fewer advertisements and more features? Click here to become a Hardware Analysis registered user.
jeremy scharlow May 04, 2013, 02:40pm EDT Report Abuse
>> Re: PHP Select drop down box
I just realized in my post i forgot to close my array and put the end line in. its there in my original code and the drop down works. It just always goes back to the first option once selected instead of staying on the selected item.

Afrow UK May 05, 2013, 07:47am EDT Report Abuse
>> Re: PHP Select drop down box
You have to explicitly add selected="selected" to the <option> that you want to select, depending on the submitted value (in $_POST). This goes for check boxes and radio buttons too (checked="checked").


I like: Programming, Gym, University
Music: House, Dubstep, D&B, Trance, Metal



  Topic Tools 
RSS UpdatesRSS Updates

  Related Articles 

A weekly newsletter featuring an editorial and a roundup of the latest articles, news and other interesting topics.

Please enter your email address below and click Subscribe.