Jump to content


Photo

Date input


  • Please log in to reply
2 replies to this topic

#1 collin84

collin84
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 15 April 2006 - 09:03 PM

Hi

I'm new to php and have just started using it for an assignment at university

I'm wanting to create a date input section on a website using 3 combo boxes for day, month and year

I then want to store this date in a mysql table

$month = array(1=>"January", "February", "March"........);

$day = $_POST['day'];
$year = $_POST['year'];

$query = "INSERT INTO table (date) VALUES ('$year-???-$day')";

The only question i have got is how to get the numerical value of the selected month and put it in place of "???" ?

Thanks
Col

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 15 April 2006 - 10:56 PM

Display month names in the combo but use month numbers for the values

<?php
if (isset ($_POST['submit'])) {
    $day = $_POST['day'];
    $month = $_POST['month'];
    $year = $_POST['year'];

    $date = "$year-$month-$day";

    $query = "INSERT INTO table (date) VALUES ('$date')";

    echo $query;
}
?>

<FORM method='POST'>
Day
<SELECT name='day'>
        <?php
        for ($d=1; $d <= 31; $d++) {
             printf ("<OPTION value='%1\$02d'>%1\$d</OPTION>", $d);
        }
        ?>
</SELECT>

Month
<SELECT name='month'>
        <?php
        for ($m=1; $m <= 12; $m++) {
             $mname = date('F', mktime(0,0,0,$m,1,2006));
             printf ("<OPTION value='%02d'>%s</OPTION>", $m, $mname);
        }
        ?>
</SELECT>

Year
<SELECT name='year'>
        <?php
        for ($y=2001; $y <= 2006; $y++) {
             echo "<OPTION value='$y'>$y</OPTION>\n";
        }
        ?>
</SELECT>

<INPUT TYPE='SUBMIT'  name='submit' value='Submit'>
</FORM>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 collin84

collin84
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 17 April 2006 - 11:11 AM

Thanks for the help, its working fine now. Thought it must have been something simple but couldn't figure it out and was spending far too long trying.

Col




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users