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
https://forums.phpfreaks.com/topic/196948-keep-drop-down-value-on-refresh/
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>

 


<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?

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>';

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.