Reputation: 3004
I have a textual month, day and year in an input field and I'd like to convert it to a date that will later be inserted into a 'date' field within a MySQL Table.
Textual month, day and year from HTML input field: "Aug 25, 2014".
PHP:
$time_cell_row = 1;
$time_cell_column = 1;
echo "<form name='timesubmit" . $time_cell_row . "' action='enter_time.php?action=timesubmit" .$time_cell_row . "' method='post'>";
$todays_date = strtotime("today"); //
$FormattedCurrDate = date('M d, Y', $todays_date); // Converts to "Aug 25, 2014"
echo "<th><input name=daycell1 type=text value=" . $FormattedCurrDate . "<disabled /></th>";
// The above echo statement displays "Aug" in a table cell instead of "Aug 25, 2014"
echo "<td><input name=submit_time" . $time_cell_row . $time_cell_column . " type=submit></input></td>";
echo "</form></tr>";
$date1 = $_POST['daycell1']; // null
echo "Date from input field: " . $date1; // Nothing is displayed from $date1 since is null
echo "<br>";
Why is $_POST['daycell1'] showing up as null instead of putting in "Aug 25, 2014"?
Upvotes: 1
Views: 1395
Reputation: 41885
Of course, $date1
is null
because you haven't submitted the form yet, so $_POST['daycell1']
is still undefined upon first load.
<input />
tags are void tags, they do not have a closing tag. Use the value=""
attribute if you want them to contain a value.
Do not use the disabled
attribute. All input tags with the disabled
attribute will not be included inside $_POST
. Use readonly
instead.
// defaults to current day
$FormattedCurrDate = date('M d, Y');
echo '<form method="POST">';
echo '<table><tr>';
echo "<th><input name='daycell1' type='hidden' value='$FormattedCurrDate' /></th>";
echo '<td><input type="submit" name="submit" /></td>';
echo '</tr></table>';
echo '</form>';
// if submit button is pressed
if(isset($_POST['submit'])) {
$date = $_POST['daycell1']; // assign the hidden tag value to variable
echo $date;
}
The date is truncated because you're missing closing quotes.
echo "<th><input name=daycell1 type=text value='" . $FormattedCurrDate . "' /></th>";
// ^ quotes ^ quotes
Sidenote: Always turn on error reporting on development.
error_reporting(E_ALL);
ini_set('display_errors', '1');
Upvotes: 2