Jump to content


Photo

Pre-selecting in a drop-down menu


  • Please log in to reply
3 replies to this topic

#1 MikoMak

MikoMak
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 31 May 2006 - 09:05 AM

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.

#2 massive

massive
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 31 May 2006 - 09:15 AM

Try this:


Drop-down for Month:

<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
Drop-down for Day:
<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...

Drop-down for year
<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>


Hope this helps

#3 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 31 May 2006 - 09:26 AM

MONTH:
<?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>
DAY:
<select name="day"><?php
  for ($i=1;$i<32;$i++) {
    echo '<option value='.$i.'"';
    if ($day==$i) {echo ' selected';}
    echo '>'.$i.'</option>';
  }
?></select>
YEAR:
<select name="year"><?php
  for ($i=1900;$i<2007;$i++) {
    echo '<option value='.$i.'"';
    if ($year==$i) {echo ' selected';}
    echo '>'.$i.'</option>';
  }
?></select>
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.
Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#4 MikoMak

MikoMak
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 01 June 2006 - 11:13 AM

Hey guys,

thanx for that! I tried Yesideez's array and that worked when I used just int for the month rather than the name.
In any case I'm inserting the values in the db in 0000-00-00 format so that's probably best anyway! :)

thanx again!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users