Jump to content

[SOLVED] make selected option stay selected in a combo after pressing a submit type butto


ferpadro

Recommended Posts

Well, the topic name says it everything. I have 3 comboboxes, one for the day, other for the month and another one for the year. Every time i select a value for the three of them and press the "Submit" button, they reset to the default option. Anyway this can be solved?

 

Thanks in advance

Link to comment
Share on other sites

<select name="selectbox">
<option value="Select">Select</option>
<option value="Option 1" <?php if(isset($_POST['selectbox'])){ echo 'selected="selected"';} ?> >Option 1</option>
</select>

 

use an if statement to check to see if the $_POST value is valued...if it is then echo 'selected="selected"' to keep the selection when the form reloads.  this can work for input fields as well just echo the value of $_POST into the 'value' attribute.

 

hope this helps

Link to comment
Share on other sites

I would do something like this..

 

<?php
$year=$_POST['year'];
$day=$_POST['day'];
$month=$_POST['month'];
$months=array(
			'1'=>'January',
			'2'=>'February',
			'3'=>'March',
			'4'=>'April',
			'5'=>'May',
			'6'=>'June',
			'7'=>'July',
			'8'=>'August',
			'9'=>'September',
			'10'=>'October',
			'11'=>'November',
			'12'=>'December'
);
?>
<select name="month" size="1">
               <option value="" selected="selected">-Month-</option>
               		<?php 

					foreach($months as $k=>$v)
					{
						if(isset($month) && $month==$k)
						{
							echo '<option value="'.$k.'" selected="selected" >'.$v.'</option>';
						}
						else
						{
							echo '<option value="'.$k.'" >'.$v.'</option>';
						}
					}


				 ?>
                        </select>
               <select name="day" size="1">
                 <option value="" selected="selected">-Day-</option>
					<?php 
					$x=1;
					while($x <= 31)
					{
						if(isset($day) && $day==$x)
						{
							echo '<option value="'.$x.'" selected="selected">'.$x.'</option>';
						}
						else
						{
							echo '<option value="'.$x.'" >'.$x.'</option>';
						}
					$x++;
					}
					 ?>
			 </select>
               <select name="select_year">
                 <option value="" selected="selected">-Year-</option>
			 <?php 
			 $y=date('Y',mktime());//get current year
			 $x=$y-100;// determine 100 years ago
			 while($y > $x) // loop through 100 years
			 {
					if(isset($year)&& $year==$year)
					{
						echo '<option value="'.$y.'" selected="selected">'.$y.'</option>';
					}
					else
					{
						echo '<option value="'.$y.'" >'.$y.'</option>';
					}

				$y--;
			 }

			 ?>
                              </select>

 

A little long winded I know, but it should do what you want. I like to use an array for most of my date based forms. We know how many months are in a year, so put it in an array, we know that no month is going to have over 31 days, so create a loop for it, and we can determine the year and just display the number of years back we want using a loop. I don' like to hand code all those values in the darn form.

 

Hope this works for ya.

 

p.s. Sorry about the funky formatting in my code. Dunno why it did that. Don't look like that in my script. :)

 

Nate

Link to comment
Share on other sites

  • 5 years later...

Thank you Chronister for your reply.

Just one note :

 

 

if(isset($year)&& $year==$year)
 

 

 

should be

 

 

if(isset($year)&& $year==$y)
 

 

 

or else all the years are selected & default selected is always last year in the list ;)

 

Thanx a million... am using this code on my site :))

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.