6

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?

2
  • first one is correct. what's the problem? Commented Jul 3, 2015 at 7:55
  • 1
    or you can just do if(empty($_POST['profession'])). This checks: - isset - is null - is '' Commented Jul 3, 2015 at 8:02

1 Answer 1

5

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

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

3 Comments

So for simplicity you would recommend this method over the two script options in my example. Would they all work as anant kumar singh determines my first code to also be correct?
Not for simplicity. Anant's statement is rather meaningless; you're actually right in your question, in that it only accepts an empty string (''). It's better to not make assumptions about what the browser will send for an empty form element, so empty() covers all possibilities of "emptiness".
Thanks aross - and well explained!

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.