Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php empty field required

Tags:

forms

php

I have a contact form which has a hidden field (profession). I am trying to get the script to check to make sure this hidden field IS empty and if so, then send the form result to me. If however this field is NOT empty, the form is not sent to me.

Orginially my code was:

    if(isset($_POST['profession']) && $_POST['profession'] == ''){

But I believe this is actually incorrect and is forcing the profession field to be blank? So I believe the code should simply be:

    if(!isset($_POST['profession'])){ 

Have I got this correct. Which would be the best way to code this?

like image 401
Michael_G Avatar asked Jul 03 '15 07:07

Michael_G


People also ask

How check input field is empty in PHP?

PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.

How can show error below input field in PHP?

To show invalid input in PHP, set the name of the input textbox which is in HTML. All the fields are first checked for empty fields and then it is validated for correctness. If all fields are correct then it shows the success message.


1 Answers

Nice to see you using the honeypot method of spam prevention. The best you can probably do is simply this:

if (empty($_POST['profession'])){ 
    // Send form result.
}

The empty() function evaluates to true when it's an empty string ('') or when the variable or array element doesn't exist at all. For more details on comparisons see here: http://php.net/manual/en/types.comparisons.php

like image 144
aross Avatar answered Oct 09 '22 00:10

aross