0

How to use PHP to check form field for specific value?

The value '7' would need to be inserted or else the form field will be invalid; so invalid with all other numbers then 7.

if(empty($_POST['captcha']))
{
    $this->add_error("Did you solve the captcha?");
    $ret = false;
}

Works to check if any value -- but I need to specify, must be 7.

if(strlen($_POST['captcha']) =!7) // tried this but form completely died or disappeared
{
    $this->add_error("Did you solve the captcha?");
    $ret = false;
}
1
  • Hey @Jack this was the answer, man. If you wanted to post Commented Sep 3, 2014 at 0:12

2 Answers 2

2

There are two things to note here:

  1. You shouldn't use empty() here, because "0" is considered empty as well and it seems like it could be the answer of an equation; use isset() instead to just check whether the value is part of the request.

  2. You're trying to check the length of $_POST['captcha'] instead of comparing its value.

So:

if (!isset($_POST['captcha']) || $_POST['captcha'] != 7) {
    $this->add_error('Did you solve the captcha?');
    $ret = false;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You have two problems:

=! should be !=. See comparison operators.

if(strlen($_POST['captcha']) != 7)

You also need to turn on error reporting.

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.