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
?
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
?
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
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.
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.
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.
If you want to do this I suggest you take the following steps:
<input type="text" name="wtv" />
with a message indicating the expected format YYYY-MM-DD
.<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.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.
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']);
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']));