Input Date From Membership Signup Form
25 posts by 4 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: April 8, 2011 (RSS)
Here’s the code we settled on (with lots of help from Jason) for anyone who want’s to implement this.
Hope it comes in handy,
Jerry Kornbluth
BACKGROUND
My client needed to list the dates of birth for her students in the accounts database.
We decided to include date fields for 3 students. Student 1, 2 and 3.
This meant that the parents would have to be able to set up the original database record and also be able to update that record when required.
THE MEMBER APPLICATION FORM
In the original application form, in the PHP code at the top of the page, in the //add user section:
$student_1_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_1'],@$_REQUEST['day_1'],@$_REQUEST['year_1']));
$student_2_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_2'],@$_REQUEST['day_2'],@$_REQUEST['year_2']));
$student_3_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_3'],@$_REQUEST['day_3'],@$_REQUEST['year_3']));
and in the mysql_query("INSERT INTO `{$TABLE_PREFIX}accounts` SET section:
student_1_dob = '".mysql_escape($student_1_dob)."',
student_2_dob = '".mysql_escape($student_2_dob)."',
student_3_dob = '".mysql_escape($student_3_dob)."',
Then in the form, the following code (in the appropriate place for your particular form):
NOTE: You only have to define the variables $lowestYear and $highestYear once per viewer.
<tr>
<td>Date of Birth - Student 1</td>
<td><?php
$lowestYear = 1920;
$highestYear = 2006;
?>
Month:
<select name="month_1">
<?php foreach(range(1,12) as $month_1): ?>
<option value="<?php echo $month_1;?>"><?php echo date("F",strtotime("0000-$month_1"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_1">
<?php foreach(range(1,31)as $day_1): ?>
<option value="<?php echo $day_1;?>"><?php echo $day_1;?></option>
<?php endforeach ?>
</select> Year:
<select name="year_1">
<?php foreach (range($lowestYear,$highestYear) as $year_1):?>
<option value="<?php echo $year_1;?>"><?php echo $year_1;?></option>
<?php endforeach?>
</select>
</tr>
<tr>
<td>Date of Birth - Student 2</td>
<td>Month:
<select name="month_2">
<?php foreach(range(1,12) as $month_2): ?>
<option value="<?php echo $month_2;?>"><?php echo date("F",strtotime("0000-$month_2"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_2">
<?php foreach(range(1,31)as $day_2): ?>
<option value="<?php echo $day_2;?>"><?php echo $day_2;?></option>
<?php endforeach ?>
</select> Year:
<select name="year_2">
<?php foreach (range($lowestYear,$highestYear) as $year_2):?>
<option value="<?php echo $year_2;?>"><?php echo $year_2;?></option>
<?php endforeach?>
</select>
</tr>
And,
<tr>
<td>Date of Birth - Student 3</td>
<td>Month:
<select name="month_3">
<?php foreach(range(1,12) as $month_3): ?>
<option value="<?php echo $month_3;?>"><?php echo date("F",strtotime("0000-$month_3"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_3">
<?php foreach(range(1,31)as $day_3): ?>
<option value="<?php echo $day_3;?>"><?php echo $day_3;?></option>
<?php endforeach ?>
</select> Year:
<select name="year_3">
<?php foreach (range($lowestYear,$highestYear) as $year_3):?>
<option value="<?php echo $year_3;?>"><?php echo $year_3;?></option>
<?php endforeach?>
</select>
</tr>
THE PROFILE UPDATE FORM
For the profile update page, the code at the top of the page, in the //update user section is:
$student_1_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_1'],@$_REQUEST['day_1'],@$_REQUEST['year_1']));
$student_2_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_2'],@$_REQUEST['day_2'],@$_REQUEST['year_2']));
$student_3_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_3'],@$_REQUEST['day_3'],@$_REQUEST['year_3']));
And the code in the $query = "UPDATE `{$TABLE_PREFIX}accounts` SET section is:
student_1_dob = '".mysql_escape($student_1_dob)."',
student_2_dob = '".mysql_escape($student_2_dob)."',
student_3_dob = '".mysql_escape($student_3_dob)."',
In the form, in order to pre-populate the individual parts of the date field, Jason came up with the following code:
<tr>
<td>Date of Birth - Student 1</td>
<td><?php
$lowestYear = 1935;
$highestYear = 2008;
//pre-populate date fields from the database
//get most current $CURRENT_USER
$CURRENT_USER = _websiteLogin_getCurrentUser();
$_REQUEST['month_1']=date("n",strtotime($CURRENT_USER['student_1_dob']));
$_REQUEST['day_1']=date("j",strtotime($CURRENT_USER['student_1_dob']));
$_REQUEST['year_1']=date("Y",strtotime($CURRENT_USER['student_1_dob']));
?>
Month:
<select name="month_1">
<?php foreach(range(1,12) as $month_1): ?>
<option value="<?php echo $month_1;?>" <?php selectedIf($month_1,@$_REQUEST['month_1']);?>><?php echo date("F",strtotime("0000-$month_1"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_1">
<?php foreach(range(1,31)as $day_1): ?>
<option value="<?php echo $day_1;?>" <?php selectedIf($day_1,@$_REQUEST['day_1']);?>><?php echo $day_1;?></option>
<?php endforeach ?>
</select> Year:
<select name="year_1">
<?php foreach (range($lowestYear,$highestYear) as $year_1):?>
<option value="<?php echo $year_1;?>" <?php selectedIf($year_1,@$_REQUEST['year_1']);?>><?php echo $year_1;?></option>
<?php endforeach?>
</select>
</tr>
<tr>
<td>Date of Birth - Student 2</td>
<td><?php
//pre-populate date fields from the database
//get most current $CURRENT_USER
$CURRENT_USER = _websiteLogin_getCurrentUser();
$_REQUEST['month_2']=date("n",strtotime($CURRENT_USER['student_2_dob']));
$_REQUEST['day_2']=date("j",strtotime($CURRENT_USER['student_2_dob']));
$_REQUEST['year_2']=date("Y",strtotime($CURRENT_USER['student_2_dob']));
?>
Month:
<select name="month_2">
<?php foreach(range(1,12) as $month_2): ?>
<option value="<?php echo $month_2;?>" <?php selectedIf($month_2,@$_REQUEST['month_2']);?>><?php echo date("F",strtotime("0000-$month_2"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_2">
<?php foreach(range(1,31)as $day_2): ?>
<option value="<?php echo $day_2;?>" <?php selectedIf($day_2,@$_REQUEST['day_2']);?>><?php echo $day_2;?></option>
<?php endforeach ?>
</select> Year:
<select name="year_2">
<?php foreach (range($lowestYear,$highestYear) as $year_2):?>
<option value="<?php echo $year_2;?>" <?php selectedIf($year_2,@$_REQUEST['year_2']);?>><?php echo $year_2;?></option>
<?php endforeach?>
</select>
</tr>
And,
<tr>
<td>Date of Birth - Student 3</td>
<td><?php
//pre-populate date fields from the database
//get most current $CURRENT_USER
$CURRENT_USER = _websiteLogin_getCurrentUser();
$_REQUEST['month_3']=date("n",strtotime($CURRENT_USER['student_3_dob']));
$_REQUEST['day_3']=date("j",strtotime($CURRENT_USER['student_3_dob']));
$_REQUEST['year_3']=date("Y",strtotime($CURRENT_USER['student_3_dob']));
?>
Month:
<select name="month_3">
<?php foreach(range(1,12) as $month_3): ?>
<option value="<?php echo $month_3;?>" <?php selectedIf($month_3,@$_REQUEST['month_3']);?>><?php echo date("F",strtotime("0000-$month_3"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_3">
<?php foreach(range(1,31)as $day_3): ?>
<option value="<?php echo $day_3;?>" <?php selectedIf($day_3,@$_REQUEST['day_3']);?>><?php echo $day_3;?></option>
<?php endforeach ?>
</select> Year:
<select name="year_3">
<?php foreach (range($lowestYear,$highestYear) as $year_3):?>
<option value="<?php echo $year_3;?>" <?php selectedIf($year_3,@$_REQUEST['year_3']);?>><?php echo $year_3;?></option>
<?php endforeach?>
</select>
</tr>
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [Jason] Input Date From Membership Signup Form
By Rusty - April 6, 2011 - edited: April 6, 2011
Hi Rusty,
You can try something like this:
Month:
<select name="month">
<?php foreach(range(1,12) as $month): ?>
<option value="<?php echo $month;?>"><?php echo date("F",strtotime("0000-$month"));?></option>
<?php endforeach ?>
</select>
Any particular reason that on CMSB v2.07 that the Month would be populating nothing but December as the Label?
<?php foreach(range(1,12) as $month): ?>
Option Value: <?php echo $month;?> - Descriptor: <?php echo date("F",strtotime("0000-$month"));?><br />
<?php endforeach ?>
Gives me this:
Option Value: 1 - Descriptor: December
Option Value: 2 - Descriptor: December
Option Value: 3 - Descriptor: December
Option Value: 4 - Descriptor: December
Option Value: 5 - Descriptor: December
Option Value: 6 - Descriptor: December
Option Value: 7 - Descriptor: December
Option Value: 8 - Descriptor: December
Option Value: 9 - Descriptor: December
Option Value: 10 - Descriptor: December
Option Value: 11 - Descriptor: December
Option Value: 12 - Descriptor: December
So I can tell the foreach is working properly, but for some reason it's displaying the month improperly.
Re: [Rusty] Input Date From Membership Signup Form
By Jason - April 6, 2011
I've found sometimes that the date function has trouble when you don't have leading zeros in front of numbers less than 10 (for example using 2 instead of 02).
Try something like this:
Month:
<select name="month">
<?php foreach(range(1,12) as $month): ?>
<?php if($month < 10) { $month = "0".$month; } ?>
<option value="<?php echo $month;?>"><?php echo date("F",strtotime("0000-$month"));?></option>
<?php endforeach ?>
</select>
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Input Date From Membership Signup Form
By Rusty - April 8, 2011
Option Value: 01 - Label: December
Option Value: 02 - Label: December
Option Value: 03 - Label: December
Option Value: 04 - Label: December
Option Value: 05 - Label: December
Option Value: 06 - Label: December
Option Value: 07 - Label: December
Option Value: 08 - Label: December
Option Value: 09 - Label: December
Option Value: 10 - Label: December
Option Value: 11 - Label: December
Option Value: 12 - Label: December
I wonder why the month in numeric form is working but the label isn't.
Re: [Rusty] Input Date From Membership Signup Form
By robin - April 8, 2011
The menu code above works for me, but different systems can treat the year "0000" differently. So here is a slightly modified version that should hopefully work to get your labels correct.
Month:
<select name="month">
<?php foreach(range(1,12) as $month): ?>
<?php if($month < 10) { $month = "0".$month; } ?>
<option value="<?php echo $month;?>"><?php echo date("F",strtotime("2010-" . $month . "-01"));?></option>
<?php endforeach ?>
</select>
Hope that helps,
Robin
Programmer
interactivetools.com