views:

80

answers:

8

Continuing from my earlier question, how can I combine the month, day, and year <select>s into a single "date" value, so that it arrives at my PHP script as

$_POST['date'], in MySQL format YYYY-MM-DD?

+1  A: 

you can create a hidden field, and fill it before form submission

kgb
+5  A: 

You could use a hidden field and build its value onSubmit, but if I were you, I'd simply use the array notation in the name attribute, and implode the array with '-' as glue : like this

<select name="date[year]">
...
<select name="date[month]">
...
<select name="date[day]">
...

php:

$date = implode('-', $_POST['date'])
//validate date format here
greg0ire
+2  A: 

The point of collecting this data separately is to ensure its format is correct. If you submit a complete date to your php script, the validation options for the php script are much more involved. Better to submit the 3 form fields, validate them the easy way and then create the date in php.

If you insist on submitting a complete date, a hidden field (as @kgb says) is probably the easiest way.

dnagirl
+5  A: 

Combine it in your PHP script, there is no reason to add a dependency on JavaScript for this. Build on things that work. Additionally, if you do it on the server, it will not be subject to as much potential malicious or accidental interference.

David Dorward
Well, the project is already (necessarily) JavaScript dependent, and there is no way around it.
Austin Hyde
A: 

You could either use javascript or php itself to do this, but there's no native xhtml means by which this is possible (so far as I'm aware).

If you use javascript to effect this (using hidden fields to submit the data to your php script) you'll still have to verify the field on the server-side before using php to process or insert the data to the database. For that reason I'm going to skip over the javascript option, and go straight for the php approach that I'd use.

I'm assuming numerical data of the form dd for day, mm for month and yyyy for year:

$day = $_POST['day']; // assume 23
$month = $_POST['month']; // assume 06
$year = $_POST['year']; // assume 2010

$mysql_date_format = $year . "-" . $month . "-" . $day; // should result in 2010-06-23

Use $mysql_date_format in your script(s) as you require.

David Thomas
+1  A: 

If you want to do this I suggest you take the following steps:

  1. Write your HTML page so that even without JS things make sense - have a normal <input type="text" name="wtv" /> with a message indicating the expected format YYYY-MM-DD.
  2. Then add JavaScript to make that <input type="text" name="wtv" /> to a type="hidden"; also add three <select> elements - one for day, month and year. Don't give these <select> elements name attributes.
  3. You'll need some JS to ensure that as the user selects different months, impossible dates don't appear; so if I select 'Feb' in months, the days past 28 should not be available; but if it's a leap year...
  4. Add an onchange event handler to those three <select> elements so that when they're modified, you change the value of your original <input type="hidden" name="wtv" /> to reflect the selected values.

When the form is submit, since the <select> elements don't have name attributes their info won't be sent to the server, but your hidden <input /> does have a name, so it will be sent, formatted as a date.

I've actually written a widget like this before... I'll try to get a demo page up and link to it.

LeguRi
Don't worry about JS support. The app requires JS to be on.
Austin Hyde
+1  A: 

If you do the following you can get the result you are looking for.

<form method="post">
    <select name="date[]" id="year">
        <option>2010</option>
        <option>2011</option>
    </select>
    <select name="date[]" id="month">
        <option>8</option>
        <option>9</option>
    </select>
    <select name="date[]" id="day">
        <option>21</option>
        <option>22</option>
    </select>
    <input type="submit">
</form>

The resultant $_POST var holds the following

Array
(
    [date] => Array
        (
            [0] => 2011
            [1] => 9
            [2] => 22
        )
)

Then all you need to do is $date = implode('-', $_POST['date']);

Nalum
Great idea! However, I need to have a bunch of these on one page, so I would need to specify the index explicitly: `date[0][]`, `date[1][]`, etc which is too complicated to be worth it.
Austin Hyde
A: 

I noticed from your original post that you are using the Datepicker() plugin for jQuery to get the date in the first place. Why not just set the default date format to be 'yy-mm-dd'?

If you want to verify the format when it gets to PHP, use

date('Y-m-d', strtotime($_POST['date']));
Joseph
No, I am *not* using any plugins. Besides that, the reason I'm looking at this is because Safari won't correctly parse YY-MM-DD or YYYY-MM-DD.
Austin Hyde
The reason I assumed you were was that you mentioned this was followup to a previous question in which you were asking about the Datepicker plugin. I apologize for the confusion. Also,as a heads-up, I found last week that IE has a difficult time with dates if the month is numerical (2010-06-23 vs. 2010-Jun-23).
Joseph