Jump to content

Recommended Posts

Hi,

I have a simple form with validation so if a user has not filled in a required field the form displays again on submit with all other fields filled in.

I have day-month-year drop downs and need help with setting the values that the user has already selected when the form loads again after validation so the dropdowns display them and the user doesn't need to select them again.

I'm sure I saw something similar on this forum ages ago but can't find it now.

Any help would be greatly appreciated.

Thanx.
Link to comment
https://forums.phpfreaks.com/topic/10836-pre-selecting-in-a-drop-down-menu/
Share on other sites

Try this:


Drop-down for Month:

[code]
<select name = "month">
<option value="<? echo $_POST['month']?>"><? echo $_POST['month']?></option>
<option>------------------------------------</option> <--try adding this also to seperate the selected item :)
<option value ="January">January</option>

etc....until December
[/code]
Drop-down for Day:
[code]
<select name = "day">
<option value="<? echo $_POST['day']?>"><? echo $_POST['day']?></option>
<option>------------------------------------</option> <--try adding this also to seperate the selected item :)
1 to 31
same here...
[/code]

Drop-down for year
[code]
<select name = "year">
<option value="<? echo $_POST['year']?>"><? echo $_POST['year']?></option>
<option>------------------------------------</option> <--try adding this also to seperate the current selected item :)
<option value ="1905">1905</option>
[/code]


Hope this helps
MONTH:[code]<?php
  $months=array("January","February","March","April","May","June","July","August","September","October","November","December");
?>
<select name="month"><?php
  for ($i=0;$i<12;$i++) {
    echo '<option value='.$months[$i].'"';
    if ($month==$months[$i]) {echo ' selected';}
    echo '>'.$months[$i].'</option>';
  }
?></select>[/code]
DAY:[code]<select name="day"><?php
  for ($i=1;$i<32;$i++) {
    echo '<option value='.$i.'"';
    if ($day==$i) {echo ' selected';}
    echo '>'.$i.'</option>';
  }
?></select>[/code]
YEAR:[code]<select name="year"><?php
  for ($i=1900;$i<2007;$i++) {
    echo '<option value='.$i.'"';
    if ($year==$i) {echo ' selected';}
    echo '>'.$i.'</option>';
  }
?></select>[/code]Thats the way I'd do it as $month would be the value obtained from the user earlier in the script. Can't guarantee it works as thats off the top of my head but it gives the general idea. The code above also makes all the data for all the drop-down boxes so there's no need to worry about making loads of HTML.
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.