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
https://forums.phpfreaks.com/topic/226135-retain-drop-down-selected-values/
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>

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.