Input Date From Membership Signup Form

25 posts by 4 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: April 8, 2011   (RSS)

By Rusty - December 14, 2010 - edited: December 14, 2010

I've already searched and came up empty handed. I'm trying to figure out how to allow a user to input a date field into the cms, not in plain-text, but from a drop-down list selector, where they pick the month, then day, then year, and then have those values entered into the User Accounts page/table upon form submission.

Any help would be greatly appreciated.
Rusty

Re: [Rusty] Input Date From Membership Signup Form

By Jason - December 14, 2010

Hi Rusty,

You can try something like this:

First we can make the form fields:

<?php
$lowestYear = 1980;
$highestYear = 2010;
?>

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>


Day:
<select name="day">
<?php foreach(range(1,31)as $day): ?>
<option value="<?php echo $day;?>"><?php echo $day;?></option>
<?php endforeach ?>
</select>


Year:
<select name="year">
<?php foreach (range($lowestYear,$highestYear) as $year):?>
<option value="<?php echo $year;?>"><?php echo $year;?></option>
<?php endforeach?>
</select>


You can set $lowestYear and $highestYear to whatever range you want.

Then when the form is submitted, you can format the input from the form like this:
$date = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month'],@$_REQUEST['day'],@$_REQUEST['year']));

You can then use $date in your sql string.

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 - December 14, 2010 - edited: December 14, 2010

That's what I had initially thought about how to do it.

The next question, directly related to the above, and my mental block @ the moment...

This is the block of code I have so far that is successfully dumping the data into the CMS, well except the very last line, the Birthday one...

I get an Undefined Index Notice:
Notice: Undefined index: birthday in /html/test.php on line 60

mysql_query("INSERT INTO `{$TABLE_PREFIX}accounts` SET
first_name = '".mysql_escape( $_REQUEST['first_name'] )."',
last_name = '".mysql_escape( $_REQUEST['last_name'] )."',
email = '".mysql_escape( $_REQUEST['email'] )."',
username = '".mysql_escape( $_REQUEST['username'] )."',
password = '".mysql_escape( $_REQUEST['password'] )."',
address_1 = '".mysql_escape( $_REQUEST['address_1'] )."',
address_2 = '".mysql_escape( $_REQUEST['address_2'] )."',
city = '".mysql_escape( $_REQUEST['city'] )."',
state = '".mysql_escape( $_REQUEST['state'] )."',
zip = '".mysql_escape( $_REQUEST['zip'] )."',
home_phone = '".mysql_escape( $_REQUEST['home_phone'] )."',
cell_phone = '".mysql_escape( $_REQUEST['cell_phone'] )."',
age = '".mysql_escape( $_REQUEST['age'] )."',
line 60 >> birthday = '".mysql_escape( $_REQUEST['birthday'] )."',


Here is the bit where the pull-downs do their work. Something tells me the Very last line in this is what's wrong.. I'm pretty sure I'm assigning the date filled out by the user improperly to $birthday.
<tr>
<td>Birthday</td>
<td><?php
$lowestYear = 1920;
$highestYear = 2006;
?>
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> Day:
<select name="day">
<?php foreach(range(1,31)as $day): ?>
<option value="<?php echo $day;?>"><?php echo $day;?></option>
<?php endforeach ?>
</select> Year:
<select name="year">
<?php foreach (range($lowestYear,$highestYear) as $year):?>
<option value="<?php echo $year;?>"><?php echo $year;?></option>
<?php endforeach?>
</select>
<?php $birthday = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month'],@$_REQUEST['day'],@$_REQUEST['year']));?>
</tr>

Rusty

Re: [Rusty] Input Date From Membership Signup Form

By Jason - December 15, 2010

Hi Rusty,

The line where you're assigning a value to $birthday should only take place after the for has been submitted. Try this:

$birthday = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month'],@$_REQUEST['day'],@$_REQUEST['year']));

mysql_query("INSERT INTO `{$TABLE_PREFIX}accounts` SET
first_name = '".mysql_escape( $_REQUEST['first_name'] )."',
last_name = '".mysql_escape( $_REQUEST['last_name'] )."',
email = '".mysql_escape( $_REQUEST['email'] )."',
username = '".mysql_escape( $_REQUEST['username'] )."',
password = '".mysql_escape( $_REQUEST['password'] )."',
address_1 = '".mysql_escape( $_REQUEST['address_1'] )."',
address_2 = '".mysql_escape( $_REQUEST['address_2'] )."',
city = '".mysql_escape( $_REQUEST['city'] )."',
state = '".mysql_escape( $_REQUEST['state'] )."',
zip = '".mysql_escape( $_REQUEST['zip'] )."',
home_phone = '".mysql_escape( $_REQUEST['home_phone'] )."',
cell_phone = '".mysql_escape( $_REQUEST['cell_phone'] )."',
age = '".mysql_escape( $_REQUEST['age'] )."',
birthday = '".mysql_escape($birthday)."',


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 - December 15, 2010

That worked wonderfully. Thanks for the continued excellent support!
Rusty

Re: [Jason] Input Date From Membership Signup Form

Hi Rusty and Jason,

Thanks for this useful post.

I’ve been trying to implement this concept in a membership application and have a few challenges.

1) I’ve got the signup form working with 1 date field called student_1_dob, but I’d ultimately like to use 3 date fields called student_1_dob, student_2_dob, and student_3_dob.

How would change the form code (below) to accommodate the 3 different date fields.

2) How would I implement the same process in my profile update form.

Here’s what I’m using in the member signup viewer for the one date.

In the PHP code at the head of the page:
$student_1_dob = date("m-d-Y",mktime(0,0,0,@$_REQUEST['month'],@$_REQUEST['day'],@$_REQUEST['year']));

$student_1_dob = date("m-d-Y",mktime(0,0,0,@$_REQUEST['month'],@$_REQUEST['day'],@$_REQUEST['year']));

$student_1_dob = date("m-d-Y",mktime(0,0,0,@$_REQUEST['month'],@$_REQUEST['day'],@$_REQUEST['year']));

mysql_query("INSERT INTO `{$TABLE_PREFIX}accounts` SET

student_1_dob = '".mysql_escape($student_1_dob)."',
student_2_dob = '".mysql_escape($student_2_dob)."',
student_3_dob = '".mysql_escape($student_3_dob)."',

... other fields to insert...")

or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
$userNum = mysql_insert_id();

And so far, in the input form in the Student 1 area only:

<tr>
<td>Date of Birth</td>
<td><?php
$lowestYear = 1920;
$highestYear = 2006;
?>
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> Day:
<select name="day">
<?php foreach(range(1,31)as $day): ?>
<option value="<?php echo $day;?>"><?php echo $day;?></option>
<?php endforeach ?>
</select> Year:
<select name="year">
<?php foreach (range($lowestYear,$highestYear) as $year):?>
<option value="<?php echo $year;?>"><?php echo $year;?></option>
<?php endforeach?>
</select>

</tr>


Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







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

Thanks Jason,

I'll give it a go and let you know

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Input Date From Membership Signup Form

Hi Jason,

I think I've done what you suggested, but the field in the update form doesn’t seem to pull the existing data from the database.

On submit, it wipes out the existing data and updates it with December 31, 1969

Thanks,

Jerry

here's the code:

At the top of the page:
$student_1_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_1'],@$_REQUEST['day_1'],@$_REQUEST['year_1']));

and

student_1_dob = '".mysql_escape($student_1_dob)."',


In the form:
<tr>
<td>Date of Birth</td>
<td><?php
$lowestYear = 1920;
$highestYear = 2006;
?>
Month:
<select name="month_1">
<?php foreach(range(1,12) as $month_1): ?>
<option value="<?php echo htmlspecialchars(@$_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 htmlspecialchars(@$_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 htmlspecialchars(@$_REQUEST[$year_1]);?>"><?php echo $year_1;?></option>
<?php endforeach?>
</select>

</tr>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Input Date From Membership Signup Form

By Jason - January 18, 2011

Hi Jerry,

There are a couple of things happening with your code. In your foreach loop, your outputting the request variable (ie, the submitted value). Since the first time this runs, there is no submitted value, it's impossible for a value to get into your drop down. This is why you're getting the same answer every time.

Also, if you want to maintain state between submissions, or to select certain values by default, you can use the selectedIf() function like this:

<tr>
<td>Date of Birth</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 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>


So if you want to set a value, you just need to set the @$_REQUEST variables at the top.

Give this a try and let me know if you run into any problems.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/