dudejma Posted June 26, 2011 Share Posted June 26, 2011 I'm wondering how to save drop down menu values. For example, say someone was filling out the form and they left a field blank, it would reload the page, and say please fill in this field or whatever, but how do I get it to save the drop down menu data. I know how to get it to save the input fields, just not the drop down menus. Quote Link to comment https://forums.phpfreaks.com/topic/240460-remember-form-data-on-reload/ Share on other sites More sharing options...
perky416 Posted June 26, 2011 Share Posted June 26, 2011 Hi mate, I use something similar to the following: <select name="dropdown"> <option value="value 1" <?php if $_POST['dropdown'] == "value 1" echo "selected"; ?>>value 1</option> <option value="value 2" <?php if $_POST['dropdown'] == "value 2" echo "selected"; ?>>value 2</option> <option value="value 3" <?php if $_POST['dropdown'] == "value 3" echo "selected"; ?>>value 2</option> </select> Hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/240460-remember-form-data-on-reload/#findComment-1235088 Share on other sites More sharing options...
dudejma Posted June 26, 2011 Author Share Posted June 26, 2011 I'm very new to PHP. Could you explain the code to me please? Quote Link to comment https://forums.phpfreaks.com/topic/240460-remember-form-data-on-reload/#findComment-1235089 Share on other sites More sharing options...
mgoodman Posted June 26, 2011 Share Posted June 26, 2011 @op: Perky's code contained some syntax errors. Here's the fixed version: <select name="dropdown"> <option value="value 1" <?php if ($_POST['dropdown'] == 'value 1') echo 'selected="selected"'; ?>>value 1</option> <option value="value 2" <?php if ($_POST['dropdown'] == 'value 2') echo 'selected="selected"'; ?>>value 2</option> <option value="value 3" <?php if ($_POST['dropdown'] == 'value 3') echo 'selected="selected"'; ?>>value 2</option> </select> edit: explanation $_POST['dropdown'] is where the selected value would be stored when the user submits the form. So, for each option the code checks to see if it was selected, and if it was it prints out selected="selected" (which will set that as the "default" option). If that doesn't make sense then let me know and I'll try to explain it differently. Quote Link to comment https://forums.phpfreaks.com/topic/240460-remember-form-data-on-reload/#findComment-1235091 Share on other sites More sharing options...
perky416 Posted June 26, 2011 Share Posted June 26, 2011 Sorry I typed it out quickly. <option value="value 1" <?php if ($_POST['dropdown'] == 'value 1') echo 'selected="selected"'; ?>>value 1</option> The php in the above line is basically saying "if the posted value is the same as the option value, display 'selected=selected' within the line of html". If an <option> tag reads <option selected="selcted"> then that dropdown value is displayed by default when the page loads. I hope you understand that. I get it in my head im just not sure if I put it across too good lol. Quote Link to comment https://forums.phpfreaks.com/topic/240460-remember-form-data-on-reload/#findComment-1235098 Share on other sites More sharing options...
dudejma Posted June 26, 2011 Author Share Posted June 26, 2011 That makes sense. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/240460-remember-form-data-on-reload/#findComment-1235099 Share on other sites More sharing options...
Pikachu2000 Posted June 26, 2011 Share Posted June 26, 2011 If you have fields with more than just a few <option>s, it's much easier to build them from arrays in php instead of coding them all into the html, if you're interested: $options = array( 'option 1', 'option 2', 'option 3', 'option 4', 'option 5'); // example array echo "<select name=\"select_options\">\n"; // start the <select> field foreach( $options as $v ) { // if the form has already been submitted, set the $selected var if there's a match, then echo each array element as an option $selected = ( isset($_POST['select_options']) && $v == $_POST['select_options'] ) ? 'selected="selected"' : ''; echo "<option value=\"$v\" $selected>$v</option>\n"; } echo "</select>\n"; // close the select field. Quote Link to comment https://forums.phpfreaks.com/topic/240460-remember-form-data-on-reload/#findComment-1235128 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.