Jump to content

PHP Form Select


himynameismark
 Share

Recommended Posts

I am setting up a registration form for a website I am about to start, and in order to help my client with some demographics, I have discussed with him setting up a Birthday field in his form. I know it's possible and not very difficult to do what I'm looking for in Javascript, but I want to know if it's possible to do this in PHP, as I am trying to use as little Javascript as possible.I have it set up as:Birthday: (drop-down select Year) (drop-down select Month) (drop-down select Day)Is it possible to determine how many days to add to the Day select list based on which month the user selects using PHP only to loop a certain amount of years and the days in the month? If not, can I use one PHP file for all three selects and create a variable for the number of days in each month, so when one is selected the number of days will change?For example, all in one file I would have

<?php	$year = date("Y");	$days_in_month = 0;	$day = 1;		// Year	echo ("<select id='year' name='year'>");	echo ("<option>Year</option>")	while ($year >= 1925)		{			echo ("<option>" . $year . "</option>");			$year--;		}	echo ("</select>");			//Month	echo ("<select id='month' name='month'>");	echo ("<option>Month</option>");	echo ("<option>January</option>");	echo ("<option>February</option>");	echo ("<option>March</option>");	echo ("<option>April</option>");	echo ("<option>May</option>");	echo ("<option>June</option>");	echo ("<option>July</option>");	echo ("<option>August</option>");	echo ("<option>September</option>");	echo ("<option>October</option>");	echo ("<option>November</option>");	echo ("<option>December</option>");	echo ("</select>");		//Day	echo ("<select id='day' name='day'>");	if ($days_in_month > 0 && $days_in_month != 28)		{			while ($day <= $days_in_month)				{					echo ("<option>" . $day . "</option>");					$day++;				}		}	elseif ($days_in_month > 0 && $days_in_month = 28)		{			if ($year / 4 = 0			!($year / 100 = 0 && $year / 400 != 0))				$days_in_month = 29;			else				$days_in_month = 28;							while ($day <= $days_in_month)				{					echo ("<option>" . $day . "</option>");					$day++;				}		}	else		echo ("<option>Day</option>");				echo ("</select>");?>

Except in the Month select, I will also include the $days_in_month declaration for each month. If this is possible, where exactly would I include that for each month? Even if that is not possible, and I have to resort to Javascript, with the last nested if statement (where I determined whether or not it is leap year), is there a better way to write that code? Or did I get it right? For those who don't know, it's any year that is divisible by 4 but not divisible by 100 UNLESS it is also divisible by 400.Thanks in advance,Mark

Link to comment
Share on other sites

First, stop thinking about using as little JavaScript as possible. There are good reasons to use it and good reasons not too, but if you use it at all there is no reason not to use as much as you need.It would be very user friendly to have a mechanism that adjusts the dates to the month right there in JavaScript so you have instant results.Otherwise, you'll need to reload the form.There are probably easier ways to do what you want using PHP's date functions.EDIT: The Shadow knows . . .

Edited by Deirdre's Dad
Link to comment
Share on other sites

First, stop thinking about using as little JavaScript as possible. There are good reasons to use it and good reasons not too, but if you use it at all there is no reason not to use as much as you need.It would be very user friendly to have a mechanism that adjusts the dates to the month right there in JavaScript so you have instant results.
Agreed.
EDIT: The Shadow knows . . .
The Shadow knows many things... :) :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...