Jump to content

Keep drop down value on refresh


sean04

Recommended Posts

Hey! So I have a register form and there are a few drop down lists on it. If a user selects a month for example (say June) and hits the register button, if there are any errors on the page when the page validates the drop down lists will go back to its original selection value (January). What is the easiest way to keep a users selection even if the page is submitted or refreshed? Hope thats clear :P

 

Thanks,

Sean

Link to comment
Share on other sites

Sorry I meant submit :P

This is what I have now.

 

<p <?php if(in_array("Month", $errors)){echo 'class="error"';} ?>>
<label for="Month">Birthday:</label>
<select name="Month" value="<?php echo $Month ?>">
<option value="">Choose...</option>
<?PHP
foreach ($months as $value) {
echo '<option value="'.$value.'">'.$value.'</option>';
} 
?>
</select>
</p>

 

As of now, if choose is selected when the user hits submit, there will be an error telling the user to choose a month. If the user chooses a month and hits submit but there is another error, the value choose will automatically be the selected value in the drop down list once again. I have done something like this but it doesn't seem to work right.

 

<p <?php if(in_array("Month", $errors)){echo 'class="error"';} ?>>
<label for="Month">Birthday:</label>
<select name="Month" value="<?php echo $Month ?>">
<option value="<?php $_POST['Month']; ?>">Choose...</option>
<?PHP
foreach ($months as $value) {
echo '<option value="'.$value.'">'.$value.'</option>';
} 
?>
</select>
</p>

 

Link to comment
Share on other sites


<p <?php if(in_array("Month", $errors)){echo 'class="error"';} ?>>
<label for="Month">Birthday:</label>
<select name="Month" value="<?php echo $Month ?>">
<?PHP
foreach ($months as $value) {
if($_POST['month'] == $months) echo ''<option value="'.$value.'" selected>'.$value.'</option>';
else echo '<option value="'.$value.'">'.$value.'</option>';
} 
?>
</select>
</p>

 

Should work?

Link to comment
Share on other sites

Thanks!

 

I'll give this a try when I get home. A question though... Where would I put "Choose"? I want the application to error when the selection is "Choose" but anything else (like Jan to Dec) is valid.

Link to comment
Share on other sites

I know its not the greatest...

 

Another thing, $months is an array of all the months so I don't think replacing it with $value is an option.

 

Thanks!

Sean

 

Look at your code carefully, replacing it with $value is the ONLY option:

 

foreach ($months as $value) {

if($_POST['month'] == $months) echo ''<option value="'.$value.'" selected>'.$value.'</option>';

else echo '<option value="'.$value.'">'.$value.'</option>';

 

The "foreach" defines $value as the first index of $months (and then loops through each index 1 by 1 redefining $value as it does).

 

foreach ($months as $value) {
if($_POST['month'] == $value) echo ''<option value="'.$value.'" selected>'.$value.'</option>';
else echo '<option value="'.$value.'">'.$value.'</option>';

Link to comment
Share on other sites

That still doesn't seem to work... I'm not sure why I can't retain values after submitting a form.

 

Here I will post what I have again.

 

$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December');

<p <?php if(in_array("Month", $errors)){echo 'class="error"';} ?>>
<label for="Month">Birthday:</label>
<select name="Month" value="<?php echo $Month ?>">
<option value="">Choose...</option>
<?PHP
foreach ($months as $value) {
echo '<option value="'.$value.'">'.$value.'</option>';
} 
?>
</select>
</p>

 

After submit as of now, the default drop down values will just appear.. not what the user previously chose.

 

Link to comment
Share on other sites

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.