I've got a problem with an input field in my php form. It looks like:
<input type="number" name="tmax" max="99" min="-99" placeholder="Temperatura max.">
I want to check whether the field is empty or not. But the problem is php considers 0
as empty.
if (empty($_POST['tmax'])) {
$tmax = null;
}else {
$tmax = $_POST['tmax'];
}
If the user leaves the input field empty the value is considered as 'null', which works perfectly. But if the user writes 0
, which is a possibility in the form, it is also treated as empty.
I've also set the default as null in SQL but the problem is, if the input is empty, the program inserts 0
in the table.
SOLUTION:
This solution works fine for me:
if ($_POST['tmax'] == "") {
$tmax = null;
}else {
$tmax = $_POST['tmax'];
}
And also with is_numeric()
if (is_numeric($_POST['tmax'])) {
$tmax = $_POST['tmax'];
}else {
$tmax = 'null';
}