Every time I try to submit the form and I have not entered nothing in the year field I get Incorrect year!
how can I still submit the form without having to enter a year. In other words leaving the year field blank and not getting a warning?
Here is the PHP code.
if(preg_match('/^\d{4,}$/', $_POST['year'])) {
$year = mysqli_real_escape_string($mysqli, $_POST['year']);
} else {
$year = NULL;
}
if($year == NULL) {
echo '<p class="error">Incorrect year!</p>';
} else {
//do something
}
if(preg_match('/^\d{4,}$/', $_POST['year'])) {
$year = mysqli_real_escape_string($mysqli, $_POST['year']);
} else if(empty($_POST['year'])){
$year = '';
} else {
$year = null;
}
if($year == NULL) {
echo '<p class="error">Incorrect year!</p>';
} else {
//do something
}
Perhaps I'm misunderstanding, but this sounds like what you want.
if(preg_match('/^\d{4,}$/', $_POST['year'])) {
$year = mysqli_real_escape_string($mysqli, $_POST['year']);
} else {
$year = false;
}
if ($year === false) {
echo '<p class="error">Incorrect year!</p>';
} else {
//do something
}
OR (if you didn't set YEAR somewhere else)
if(preg_match('/^\d{4,}$/', $_POST['year'])) {
$year = mysqli_real_escape_string($mysqli, $_POST['year']);
}
if (!isset($year)) {
echo '<p class="error">Incorrect year!</p>';
} else {
//do something
}
Hey you can just take this part out and it will take the echo out allowing you to submit with the field being blank
if($year == NULL) {
echo '<p class="error">Incorrect year!</p>';
} else {
//do something
}
I wouldn't suggest allowing it to go blank into the database. Maybe you can just grab the default date on the server or a default value meaning the user didn't fill it out. This would take the error out but still leave something in the database field.