AdrianRobinson Posted October 5, 2011 Share Posted October 5, 2011 I'm simply trying to set up a form where, if when a user clicks 'Submit', and then 'Back', the values from the form are preserved. My question is, how do I preserve the values of drop down menus. The following is a snippet of my code: <select name="dropdown_dept" id="dept_list"> <option value=0><?php echo "Please select one..."?></option> <?php $dropdown_dept = "select dept_name from departments"; $result_dept = $db_conn->query($dropdown_dept); if (!$result_dept) { echo '<p>Unable to get department data.</p>'; return false; } for($i=0; $i<$result_dept->num_rows; $i++) { $app_name_row = $result_dept -> fetch_array(); ?> <option><?php echo($app_name_row[0]); ?></option> <? } ?> </select> Above is where I have set up a drop down menu of departments. Given that code, how can I preserve the department name after a user clicks 'Submit'? Link to comment https://forums.phpfreaks.com/topic/248502-trying-to-preserve-values-of-drop-down-menus-after-submit/ Share on other sites More sharing options...
Drummin Posted October 5, 2011 Share Posted October 5, 2011 Add this between the select and first option tag. IF (isset($_POST['dropdown_dept'])){ echo "<option>$_POST[dropdown_dept]</option>"; } Link to comment https://forums.phpfreaks.com/topic/248502-trying-to-preserve-values-of-drop-down-menus-after-submit/#findComment-1276136 Share on other sites More sharing options...
AdrianRobinson Posted October 6, 2011 Author Share Posted October 6, 2011 That worked! Thanks! Link to comment https://forums.phpfreaks.com/topic/248502-trying-to-preserve-values-of-drop-down-menus-after-submit/#findComment-1276496 Share on other sites More sharing options...
Pikachu2000 Posted October 6, 2011 Share Posted October 6, 2011 Your <option> tags should have a value= attribute to be valid markup. Link to comment https://forums.phpfreaks.com/topic/248502-trying-to-preserve-values-of-drop-down-menus-after-submit/#findComment-1276499 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.