0

I'm checking to make sure all incoming $_POST values are not empty. is using isEmpty the best method. This is a second check prior to db entry, I'm already using a jquery form validate

if (!isEmpty($_POST['add']) || ($_POST['add']['type'] == 2 && !isEmpty($_POST['cpl']))):
    // insert into db
else:
    header("Location: /?req=register");
    exit();
endif;
2
  • 1
    I think you are thinking of !empty($_POST['add']) php.net/manual/en/function.empty.php Commented Jan 26, 2013 at 5:30
  • @Moak thanks, not sure what i was thinking when I typed that Commented Jan 26, 2013 at 5:34

3 Answers 3

6

try this

if (isset($_POST['add']) AND $_POST['add'] != '')

you can chcek in many way using also NULL

Sign up to request clarification or add additional context in comments.

Comments

1

Be careful, as empty (no such thing as isEmpty) considers some values as empty.

The following things are considered to be empty:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)

You can just use if($_POST['add'] <> '') ...

5 Comments

thanks, what should I use instead of !Empty to check if the values and nothing in them?
You can just use if($_POST['add'] <> '') ...
something like this would work fine correct? if ($_POST['add'] != '' || ($_POST['add']['type'] == 2 && $_POST['cpl'] != '')):
@njk Wouldn't if ($_POST['add'] <> '') throw a php notice if it is undefined?
@Antony I'm assuming it would be defined and depends on the error reporting.
0

you can use isset to see whether the variable is set or not. This will help in radio and checkbox if you have not selected.

 (isset($_POST['add']) && $_POST['add'] != '')

You can also use !empty($_POST['add'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.