Jump to content

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

 

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.