Jump to content

Retain drop down selected values


Bravat

Recommended Posts

I have several drop down boxes. When submit is clicked it execute query on another page. Problem is when submit is clicked selected values are return to defaults. Is it possible for the values to be remembered after submitting them? Part of the drop down code:

  <?php
            $result = mysql_query("SELECT *
FROM product
GROUP BY Rim");
            ?>
            
  <select id="Rim" name="Rim"  selected="selected" style="width:158px;position:static;z-index:-1;">
  <option value="rim" selected="selected"><?php echo "Rim"; ?></option>

<?php

while ($row = mysql_fetch_array($result)) {
  echo "<option>" . $row['Rim']  . "</option>";
  echo "<br />";
  } ?>
  
       </select> 

 

Link to comment
Share on other sites

When an <option> tag has no name= attribute, the value of the <select> in which it resides is empty in the $_POST array with most browsers. Some will take the value of the label and use it as the value in the $_POST array, but that still isn't valid markup.

 

This should make that field's markup valid and retain the value in the field when the form is submitted.

<select id="Rim" name="Rim"  selected="selected" style="width:158px;position:static;z-index:-1;">
  <option value="rim" <?php echo (empty($_POST['Rim']) || $_POST['Rim'] == 'rim') ? 'selected="selected"' : ''; ?>>Rim</option>

<?php

while ($row = mysql_fetch_array($result)) {
  echo "<option value=\"{$row['Rim']}\"";
  echo (!empty($_POST['Rim']) && $_POST['Rim'] == $row['Rim']) ? ' selected="selected"' : '';
  echo ">{$row['Rim']}</option>\n";
}
?>
</select>

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.