Jump to content

[SOLVED] Remember Drop Down Value Selected


cozzy1984

Recommended Posts

Hi was, just wondering if anyone knew how to return the value a user selects from a drop down menu if the form doesn't validate. Need this as i have the form doing various checks after the form is submitted then if there is an error they all appear and i want all the values entered/selected the first time to reappear. I have the code for returning input values value="<?php echo $_POST['username']; ?>" but just dont know what the best way to tackle the drop down menu.

 

My code is:

<fieldset>
<label for="">Location:</label>
<select name="location" onfocus="infotitle('Enter Location'); info('Please select the location that you live in, this will be displayed in your ads.');">
<option value="Please Select">Please Select..</option>
<option value="Co.Antrim">Co.Antrim</option>
<option value="Co.Armagh">Co.Armagh</option>
<option value="Co.Down">Co.Down</option>
<option value="Co.Fermanagh">Co.Fermanagh</option>
<option value="Co.Londonderry">Co.Londonderry</option>
<option value="Co.Tyrone">Co.Tyrone</option>
</select>
<span class="<?php echo $locationerror; ?>"><?php echo $error['location']; ?></span>
</fieldset>

Link to comment
https://forums.phpfreaks.com/topic/89789-solved-remember-drop-down-value-selected/
Share on other sites

<fieldset>
<label for="">Location:</label>
<select name="location" onfocus="infotitle('Enter Location'); info('Please select the location that you live in, this will be displayed in your ads.');">
<option value="Please Select">Please Select..</option>
<option value="Co.Antrim"<?=$_POST['location'] == 'Co.Antrim'?'selected=true ':''?>>Co.Antrim</option>
<option value="Co.Armagh"<?=$_POST['location'] == 'Co.Armagh'?'selected=true ':''?>>Co.Armagh</option>
<option value="Co.Down"<?=$_POST['location'] == 'Co.Down'?'selected=true ':''?>>Co.Down</option>
<option value="Co.Fermanagh"<?=$_POST['location'] == 'Co.Fermanagh'?'selected=true ':''?>>Co.Fermanagh</option>
<option value="Co.Londonderry"<?=$_POST['location'] == 'Co.Londonderry'?'selected=true ':''?>>Co.Londonderry</option>
<option value="Co.Tyrone"<?=$_POST['location'] == 'Co.Tyrone'?'selected=true ':''?>>Co.Tyrone</option>
</select>
<span class="<?php echo $locationerror; ?>"><?php echo $error['location']; ?></span>
</fieldset>

It's usually easier to maintain if you do it this way:

 

<fieldset>
  <label for="">Location:</label>
  <select name="location" onfocus="infotitle('Enter Location'); info('Please select the location that you live in, this will be displayed in your ads.');">
    <option value="Please Select">Please Select..</option>
<?php
  $location_opts = array(
    "Co.Antrim",
    "Co.Armagh",
    "Co.Down",
    "Co.Fermanagh",
    "Co.Londonderry",
    "Co.Tyrone",
  );
  foreach($location_opts as $opt){
    $selected = $_POST['location'] == $opt ? " selected=true":"";
    print "<option value=\"{$opt}\"{$selected}>{$opt}</option>";
  }
?>
  </select>
  <span class="<?php echo $locationerror; ?>"><?php echo $error['location']; ?></span>
</fieldset>

If you are using the first option I sent...i noticed an error:

 

<option value="Co.Antrim"<?=$_POST['location'] == 'Co.Antrim'?'selected=true ':''?>>Co.Antrim</option>

In the above, the space should be before selected=true instead of after it. like so:

<option value="Co.Antrim"<?=$_POST['location'] == 'Co.Antrim'?' selected=true':''?>>Co.Antrim</option>

 

...change that for all of them

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.