Jump to content

Retaining Drop Down Select Values After Submitting A Form


kdigital

Recommended Posts

Hello, the following code works for all of my other selects but not the one for height. I know it has something to do with the quotes and HTML symbols. Does anyone know how to fix this? Thanks.

 

<option <?php if(isset($_POST)){if($_POST == "5'0""){echo "selected";}} ?> value="5'0"">5'0"</option>
Link to comment
Share on other sites

I would highly suggest creating a function to create and auto-select the options instead of putting the code for each and every option. I typically define my options as an array and then pass the array and the currently select value (if there is one) to a function. Also isset($_POST) is worthless since it will always be set even if there was no form posted. Lastly, I would also consider changing the values of the options to something that won't cause problems with HTML markup. The displayed label can still be with the quote marks though. In my example below, I used what 'appears' to be a decimal value, but is just the feet/inches separated by a period.

 

This is just a rough example

<?php

$height_options = array(
'5.0' => "5'0\"";
'5.2' => "5'2\"";
'5.4' => "5'4\"";
'5.6' => "5'6\"";
'5.8' => "5'8\"";
'5.10' => "5'10\"";
'5.0' => "6'0\"";
);

function createOptions($optionsAry, $selectValue=false)
{
$optionsHTML = '';
foreach($optionsAry as $value => $label)
{
    $value = htmlspecialchars($value);
    $label = htmlspecialchars($label);
    $selected = ($selectValue===$value) ? ' selected="selected"' : '';
    $optionsHTML .= "<option value='{$value}'>{$label}</option>\n";
}
return ;
}

?>

<select name="height">
<>php echo createOptions($height_options, $_POST['height']); ?>
</select>

Edited by Psycho
Link to comment
Share on other sites

A nice and concise example above. I cleaned up Psyco's code a bit from the rough example:

 

$height_options = array(
'5.0' => "5'0\"",
'5.2' => "5'2\"",
'5.4' => "5'4\"",
'5.6' => "5'6\"",
'5.8' => "5'8\"",
'5.10' => "5'10\"",
'5.0' => "6'0\"",
);

function createOptions($optionsAry, $selectValue=false)
{
$optionsHTML = '';
foreach($optionsAry as $value => $label)
{
    $label = htmlspecialchars($label);
    $selected = ($selectValue === $value) ? ' selected="selected"' : '';
    $optionsHTML .= "<option value='{$value}'{$selected}>{$label}</option>\n";
}
return $optionsHTML;
}

?>

<select name="height">
<?php echo createOptions($height_options, $_POST['height']); ?>
</select>

 

The above code will work if you follow psycos' advice and go with the decimal scheme, which will make your life much easier.

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.