Bravat Posted January 30, 2011 Share Posted January 30, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/226135-retain-drop-down-selected-values/ Share on other sites More sharing options...
Pikachu2000 Posted January 30, 2011 Share Posted January 30, 2011 Your <option> tags echoed in the while() from the DB query have no value= attribute, so the value won't even be present in the $_POST array if one of them is selected. Quote Link to comment https://forums.phpfreaks.com/topic/226135-retain-drop-down-selected-values/#findComment-1167371 Share on other sites More sharing options...
Bravat Posted January 30, 2011 Author Share Posted January 30, 2011 I am not sure that I understand this At the moment search is working fine. What I need to do? Quote Link to comment https://forums.phpfreaks.com/topic/226135-retain-drop-down-selected-values/#findComment-1167379 Share on other sites More sharing options...
iarp Posted January 30, 2011 Share Posted January 30, 2011 He said the <option> tags in your while loop do not not have a value="name-here" attribute which is needed. You also don't need selected="selected" on the Rim entry because it's the first entry it default gets selected anyways. Quote Link to comment https://forums.phpfreaks.com/topic/226135-retain-drop-down-selected-values/#findComment-1167381 Share on other sites More sharing options...
Pikachu2000 Posted January 30, 2011 Share Posted January 30, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/226135-retain-drop-down-selected-values/#findComment-1167387 Share on other sites More sharing options...
Bravat Posted January 30, 2011 Author Share Posted January 30, 2011 That is what I was looking for. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/226135-retain-drop-down-selected-values/#findComment-1167391 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.