0

I have a date of birth, and when the user is over Feb (02), the days should go only to 29. As you can see I'm using $month="1" just to test it. I'm supposed to use PHP only, no JavaScript or anything else. How would i go about making that?

<?php
$month="1"; // <-- currently using this to make it 29,30 or 31 days

print "<select name='day'>";
if  ($month==1){
    for ($i=0; $i<=28; $i++)    
    {
        $day = 1 + $i;
        print "<option value = $day>" . $day ."</option>";
    }
}
if  ($month==2){
    for ($i=0; $i<=29; $i++)
    {
        $day = 1 + $i;
        print "<option value = $day>" . $day ."</option>";
    }
}


print "</select>";

print "<select name='month'>";
for ($i=0; $i<=11; $i++)
{

    $month = 1 + $i;
    print "<option value = $month>" .$month ."</option>";
}
print "</select>";

?>

5
  • $month == 1 not $month =="1". Otherwise it is an string. Commented Aug 6, 2012 at 16:16
  • @Sable PHP evaluates $month == 1 and $month == "1" to be the same. $month === 1 and $month === "1" are different. Commented Aug 6, 2012 at 16:22
  • It's actually the same thing with a loose comparison. This would also be true: $month == "1 foo bar". If he had used the strict comparison ($month === x) however, it would be a different story. Commented Aug 6, 2012 at 16:23
  • @bob12345 you might want to use a switch statement instead of all those if statements. It won't change the functionality, but it will clean up your code a bit. Commented Aug 6, 2012 at 16:23
  • or create an array with key $month and value of number of days in that month and use just 1 piece of code like print "<select name='day'>"; for ($day=1; $day<=$n_days[$month]; $day++) print "<option value='$day'>$day</option>"; print "</select>"; Commented Aug 6, 2012 at 16:28

2 Answers 2

3

Once PHP sends stuff to the browser, it is done. It cannot affect the page in any way.

JavaScript, on the other hand, takes over when the browser gets the page. It CAN change the page, and it can even ask the server to do something (in which case PHP may get involved).

In other words, you cannot do what you are asking without JavaScript.

Sign up to request clarification or add additional context in comments.

8 Comments

@Sable i know but when u go choose month 02(feb) the day drop down should be limited to 29 only
Kolink's statement still stands. Once PHP sends the page to the browser, it can no longer modify it. That includes changing the day options after a user selects a month. If you really can't use JavaScript, then you'll have to do the date selection in two steps: one for month/year, one for day.
@Kolink OP can do it, but it would require a page refresh.
@Matt And what would trigger the page refresh?
oncli... ah i see what you did there!
|
0

Without Javascript it is just impossible to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.