Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/240460-remember-form-data-on-reload/
Share on other sites

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.

@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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.