rickphp Posted March 23, 2009 Share Posted March 23, 2009 I am currently making a php contact form, and i have various validation checks being ran, and when an error is found the data the user inputted was being lost because the page is refreshing. I managed to resolve the issue by using the coding below for input fields but I can't manage to make drop down boxes retain the selected option when the page is refreshed, any help please? <?php session_start(); ?> if ($submit) { foreach($_POST as $key=>$value){ $_SESSION[$key]=$value; } Here is an example of how the data is retained in a normal input field. <input class="form" name="name1" type="text" size="26" maxlength="20" value="<? print("$_SESSION[name1]");?>" /> But I can't figure out how to do the same for the drop down boxes? Below is the html code for the drop down box. Any help please? <select class="form" name="customer"><option value="Select One">Select One</option><option value="Yes">Yes</option><option value="No">No</option></select> Thank you! Link to comment https://forums.phpfreaks.com/topic/150773-solved-retaining-drop-down-box-information-on-reload/ Share on other sites More sharing options...
Stephen68 Posted March 23, 2009 Share Posted March 23, 2009 Not sure why it won't work did you try echoing out the $_POST value for the drop down to see if it is being lost on the way to the session or after? Link to comment https://forums.phpfreaks.com/topic/150773-solved-retaining-drop-down-box-information-on-reload/#findComment-792140 Share on other sites More sharing options...
gnawz Posted March 23, 2009 Share Posted March 23, 2009 I would rather you use javascript otherwise try employing sticky form techniques Link to comment https://forums.phpfreaks.com/topic/150773-solved-retaining-drop-down-box-information-on-reload/#findComment-792153 Share on other sites More sharing options...
rickphp Posted March 23, 2009 Author Share Posted March 23, 2009 I am trying to refrain from using java in this instance, I'm just not sure how to make the saved data show as the selected option on page refresh. Can anyone figure out the proper format? Link to comment https://forums.phpfreaks.com/topic/150773-solved-retaining-drop-down-box-information-on-reload/#findComment-792183 Share on other sites More sharing options...
lonewolf217 Posted March 23, 2009 Share Posted March 23, 2009 <select class="form" name="customer"> <option value="Select One">Select One</option> <option value="Yes" <?php if($_POST['customer']=='Yes') { echo "selected=\"selected\""; } ?>>Yes</option> <option value="No" <?php if($_POST['customer']=='No') { echo "selected=\"selected\""; } ?>>No</option> </select> Link to comment https://forums.phpfreaks.com/topic/150773-solved-retaining-drop-down-box-information-on-reload/#findComment-792186 Share on other sites More sharing options...
rickphp Posted March 23, 2009 Author Share Posted March 23, 2009 <select class="form" name="customer"> <option value="Select One">Select One</option> <option value="Yes" <?php if($_POST['customer']=='Yes') { echo "selected=\"selected\""; } ?>>Yes</option> <option value="No" <?php if($_POST['customer']=='No') { echo "selected=\"selected\""; } ?>>No</option> </select> Perfect, works a treat! Thank you! Link to comment https://forums.phpfreaks.com/topic/150773-solved-retaining-drop-down-box-information-on-reload/#findComment-792190 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.