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: [gkornbluth] Input Date From Membership Signup Form

By Jason - January 17, 2011

Hi Jerry,

You're code for creating the dates and inserting them into the mysql string looks good. What you need to change is in the HTML form. You'll need a total of 9 fields (3 for each date). So you'll need 3 month, 3 day, and 3 year drop downs.

You can pretty much just copy and paste what you have and then alter the names: month_1,day_1,year_1,month_2, etc.

After that all you need to do is change your mktime statements to use the new variables.

Is this what you're looking for? Let me know if you run into any issues.
---------------------------------------------------
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

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/