Php question on a form

Hello all,

I’m working on a little form to collect vehicle information from a site visitor. I have most of it working so far but i’m hung up on one issue. Take a look at the code block:

<tr>
     <td width="81" class="form_content"><strong>Year:</strong></td>
     <td width="175">
<?php
	if ($_POST['vehicleYear'] == '' && $_POST['vehicleYear'] == null)
	{
?>            
             <select name="vehicleYear"  class="content">
                  <option value="">Select Year...</option>
<?php
                       for ($x = date("Y") + 1; $x >= 1962; $x--) 
		       {
			     echo '<option value="'.$x.'">'.$x."</option>";
			}					
?>                  
	     </select>  
<?php
	}
		else
		{						
?>							
                	<select name="vehicleYear" class="content">
                    		<option value="<?php echo $_POST['vehicleYear'];?>"><?php echo $_POST['vehicleYear'];?></option>
                        </select>
<?php
		}
?>
</td>
</tr>

What i’m trying to do in that pathetic attempt of mine, is check to see if the year has already been selected when this form loads. If it has, I’d like the select box to retain that value, and if it has not, I’d like the select box to display a bunch of years for the user to pick from.

I just can’t think how to modify the for loop i’m using to display the list so it will check for the value of the selected year and mark that one as selected while still displaying the rest of the years… any thoughts?

Here’s a link to the form in it’s current wussy state.

Thanks in advance for any insight, tips, and constructive criticism. :slight_smile:


<tr>
     <td width="81" class="form_content"><strong>Year:</strong></td>
     <td width="175">         
    	<select name="vehicleYear"  class="content">
        	<option value="">Select Year...</option>
			<?php
            	for ($x = date("Y") + 1; $x >= 1962; $x--)  {
			   		echo '<option value="'.$x.'"'.(isset($_POST['vehicleYear']) && $_POST['vehicleYear'] == $x?' selected="selected"':'').'>'.$x."</option>";
				}
			?>              
	  	</select> 
	</td>
</tr>

The value needs to compared to the one stored in the POST array and if its equal select the option.

awesome oddz… and thanks for talkin through the logic… makes a lot of sense… thanks again!!!

J