errcricket Posted April 2, 2009 Share Posted April 2, 2009 hello. i have a form that checks for validation and does not connect w/database until is ok. for drop down menus i have 2 concerns: 1. it should retain field input for any reposts 2. if a previous option is checked, then they must select an item from the drop down menu. if not, an error message appears. <label for="X" maxlength="2">X: </label> <select id="x" name="x" maxlength="2" VALUE="<?php if(isset($_POST['x'])) echo $x; ?>"/> <option <?php if ($_POST['x'] == "-"){ echo "selected";} ?> >-</option> <option <?php if ($_POST['x'] == "y"){ echo "selected";}?> >y</option> <option <?php if ($_POST['x'] == "z"){ echo "selected";}?> >z</option> . . </select> not sure about efficiency, but this makes the values sticky. unfortunately is also has a "Notice: undefined index..." initially in the box (i will turn off the error msg later). if user choices require a selection from the menu but nothing is selected, the error message will not appear until they have tried to submit 2 times. how can i define something that has not been selected yet? i also tried below, but was unable to get it to stick at all. <label for="x" maxlength="2">X: </label> <select name="xy" size="1" maxlength="2" > <?php $xy= array ('-', 'y', 'z',... .'); print_r($xy); foreach ($xy as $optValue) { $selected = ($optValue == $_POST['x']) ? ' selected="selected"' : ''; echo "<option value=\"{$optValue}\"{$selected}>{$optValue}</option>\n"; } echo "</select>\n"; ?> </select> does anyone have any suggestions? thanks in advance. p.s. if user chooses the non-required option, the default is/should be "-" Link to comment https://forums.phpfreaks.com/topic/152259-how-to-retain-drop-down-menu-selected-state-and-display-error-if-not-selected/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.