How do I make my HTML form active?

Steeevoe

New Member
Messages
103
Reaction score
0
Points
0
I am struggling to get a date of birth selection working.

I have three fields, day, month and year.
Day and Month are select fields.


I would like the day field to change according to the month selected with the page refreshing. So I would like 30 days available when month is April, June, July, Sept and Nov, 31 days for the other months apart from February with 28 (apart from leap year)

I have tried google but I can't find a lead to how I am supposed to sovle this.

Please help!



(note I am using php method to submit form when finished)
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
Firstly, set a variable according to month chosen using a series of if, thens.

PHP:
<?php
$daysinmonth=31;//set default
if ($_POST['month'] =="Jan"){
$daysinmonth=31;
} else if ($_POST['month'] == "Feb"){
$daysinmonth=28;
}

etc... etc... for each month

Then you need your select field for the dates..

PHP:
<select name="selectdate" id="selectdate">
       <?php
	for ($i = 1; $i <= $daysinmonth; $i++)//starting at 1 and looping until daysinmonth is reached - then incrementing by 1
	{
	echo '<option value="'. $i . '" ';// value
	echo  ">" . $i . "</option>";// viewed
	}
	?>
      </select>

Haven't tested this so the sytax might be a bit off, but the principle is OK.

______________

The trouble with this is that you have to refresh - some use AJAX, which could achive this without refreshing, but I don't know enough to help on this.

Alternatively, as posted above, you could just rip one thats already done!
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
That will work too, the only problem is leap years
 

mattura

Member
Messages
570
Reaction score
2
Points
18
Look up javascript.
It's not that difficult to learn if you know php and will be able to dynamically change the fields.
Just give everything an id parameter (<input id='box1' />) and refer to the element by document.getElementById(theID) and use innerHTML
eg
HTML:
<select id='datebox'></select>
<script>
document.getElementById('datebox').innerHTML="<option>1st</option><option>2nd</option>";
</script>
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
Or you could let the user submit any date, and check on server-side...
 

gomarc

Member
Messages
516
Reaction score
18
Points
18
Assuming the user gives you the year and the month, you can also get the number of days like this:

PHP:
<?php

//Number of days for the given month and year

$month = "2";
$year  = "2008";

$days = date("t", mktime(0, 0, 0, $month, 1, $year));

// Prints: The year/month 2008/2 has 29 days!
echo "The year/month  " . $year . "/" . $month . " has " . $days . " days!";

?>

... and then build your options up to that number of days.
 

Steeevoe

New Member
Messages
103
Reaction score
0
Points
0
Thank you guys. Much apprecited. Just got to find the time to do it. We happen to be decorating at the mo ^^
 
Top