Help please, am trying to validate a form date input not empty and should not be greater that today's date. this is what I did so far. am getting 000-00-00 inserted in MySQL db. what am I doing wrong?
here is what in the form input
<div class="form-group">
<label>Join Date</label>
<input type="date" name="joindate" class="form-control <?php echo (!empty($joindate_err)) ? 'is-invalid' : ''; ?> " value="<?php echo $joindate ?>">
<span class="invalid-feedback"><?php echo $joindate_err; ?></span>
</div>
the php tag above has this validations
//date validation
$input_joindate = trim($_POST["joindate"]);
if (empty($input_joindate)) {
$joindate_err = "Select join date";
}
if (!empty($input_joindate)) {
$input_joindate = date('Y-m-d', strtotime($input_joindate));
$today = strtotime("now");
if (($input_joindate) > $today)
$joindate_err = "Date should not be in the future";
} else {
$joindate = $input_joindate;
}
My Answer:
The issue with your code is that you are not assigning the validated date back to the `$joindate` variable. You are assigning it to `$input_joindate` instead. Here is the corrected code:
php
//date validation
$input_joindate = trim($_POST["joindate"]);
if (empty($input_joindate)) {
$joindate_err = "Select join date";
} else {
$input_joindate = date('Y-m-d', strtotime($input_joindate));
$today = strtotime("now");
if ($input_joindate > $today) {
$joindate_err = "Date should not be in the future";
} else {
$joindate = $input_joindate;
}
}
By assigning the validated date back to the `$joindate` variable, the correct date value will be used when inserting into the MySQL database.
Rate this post
5 of 5 based on 5125 votesComments