Jump to content

Recommended Posts

i have the following code to collect a user date of birth and insert into mysql.i want to use the select menu for the date.what would be the best way of collecting the $_POST['data'].

$Current = explode( "-", date( "n-j-Y" ) );

$Month = $Current[0];
$Day = $Current[1];
$Year = $Current[2];

$FirstYear = 1980;
$LastYear = $Year; // Only display up to the current year - Change if you need

// Output Days
echo "<TR><TD>Day:</TD><TD><SELECT NAME='day'>\n";
for( $i = 1; $i <= 31; $i++ )
{
echo "<OPTION VALUE='" . $i . "'";
if ( $i == $Day ) echo " SELECTED";
echo ">" . $i . "</OPTION>\n";
}
echo "</SELECT></TD</TR>\n";

// Output Months
echo "<TR><TD>Month:</TD><TD><SELECT NAME='month'>\n";
for( $i = 1; $i <= 12; $i++ )
{
echo "<OPTION";
if ( $i == $Month ) echo " SELECTED";
echo ">" . date( "M", mktime( 0, 0, 0, $i, $Day, $Year ) ) . "</OPTION>\n";
}
echo "</SELECT></TD</TR>\n";

// Output Years
echo "<TR><TD>Year:</TD><TD><SELECT NAME='year'>\n";
for( $i = $FirstYear; $i <= $LastYear; $i++ )
{
echo "<OPTION VALUE='" . $i . "'";
if ( $i == $Year ) echo " SELECTED";
echo ">" . $i . "</OPTION>\n";
}
echo "</SELECT></TD</TR>\n";

?>

Link to comment
https://forums.phpfreaks.com/topic/155503-posting-date-of-birth-into-a-table/
Share on other sites

I would setup the field to be a MySQL DATE field. Then on the submitted section do something like this (assuming that you used POST as the submit method)

 

if (isset($_POST['submit'])) {
    $bday = $_POST['year'] . "-" . $_POST['month'] . "-" . $_POST['day'];
    // do the mysql insert with $bday as the field.
}

 

Before entering the date into the DB, you may want to run a checkdate on the values to make sure they are a valid date.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.