5

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';
}
LW001
  • 2,452
  • 6
  • 27
  • 36

6 Answers6

4

Check if the condition is empty, and also not zero. A zero-value is "empty", so by adding both checks, you ensure that the variable $tmax will be set to null if the input was empty and not zero.

if (empty($_POST['tmax']) && $_POST['tmax'] != 0) {
    $tmax = null;
} else {
    $tmax = $_POST['tmax'];
}

This will also accept "foo" as a value, so you should check or validate that the input is a valid number (and also in the ranges you specified). You can also implement is_numeric($_POST['tmax']), or even better, validate it with filter_var($_POST['tmax'], FILTER_VALIDATE_INT) to ensure that whatever was input is actually a number.

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • IMHO, its a confusing technique to use `empty`, then add additional tests to correct for `empty`s "too inclusive" design - if its a situation where `empty` doesn't do what is wanted, don't use it, in my opinion. I far prefer to build up from more fundamental calls, that have simpler semantics. You mention `is_numeric` - that seems the better starting point. – ToolmakerSteve Apr 02 '19 at 21:03
2

You can use !is_numeric() instead empty()

thanks for such a important note, Rafa

  • **Note:** To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use **is_numeric()**. But thanks a lot! its work for me. – Rafa cosquiere Jul 29 '17 at 16:17
1

As you state, 0 is considered empty.

The function you want is isset().

if (!isset($_POST['tmax'])) {
    $tmax = null;
} else {
    $tmax = $_POST['tmax'];
}

Alternatively, remove the not operator and switch the code blocks.

tuxedobob
  • 223
  • 1
  • 7
1

This code should work for what are you trying to get.

if (!isset($_POST['tmax']) || $_POST['tmax'] == '') {
    $tmax = null;
}else {
    $tmax = $_POST['tmax'];
}
lucianov88
  • 185
  • 3
  • 10
1

if you want to have placeholder - you can use this code:

<input type="number" name="tmax" max="99" min="-99" onclick="if (this.value == '') {this.value='0';} " placeholder="Temperatura max.">

don't forget add validation (before send form check on empty fileds )

and php to:

$tmax = 0;
if (isset($_POST['tmax'])) {
  $tmax = $_POST['tmax'];
}
Lesiuk Alexey
  • 237
  • 1
  • 2
  • 7
  • Thanks for the reply, but this solution is not esthetic for me, because the info is displayed on placeholder, and with 'value' the user can see the info. – Rafa cosquiere Jul 29 '17 at 16:21
  • Ok I change answer , now - with placeholder – Lesiuk Alexey Jul 29 '17 at 16:30
  • This is a great technique if you *want* to replace an empty field with a default value. [To clarify for future readers: Original question wanted the opposite behavior - how to distinguish between an empty value, which means "not set", and is permitted in his code, and a value of zero.] – ToolmakerSteve Apr 02 '19 at 21:09
0

you may use

if ($_POST['tmax'] == "") {
  $tmax = null;
}else {
  $tmax = $_POST['tmax'];
}
Rezbin
  • 43
  • 6
  • 1
    This will throw notices. – chris85 Jul 29 '17 at 15:53
  • See lucianov's better answer, which includes `!isset` to avoid the php warning chris mentions. Or any other answer below (except Lesiuk's, which is quite good for a different situation, but not what is asked for here). – ToolmakerSteve Apr 02 '19 at 21:15