I'm lost in how to compare values

Hi Everybody,

I have a form, and to be fancy I wanted to include a simple security snippet:

<?php $firstnumb = rand(1,9); $secondnumb = rand(1,99); $entry = $firstnumb + $secondnumb; print $firstnumb + $secondnumb ?>

Then I have a simple <input type=“text” where a visitor can simply add the value of $firstnumb to the value of $secondnumb enter the numerical answer a textbox named “entry1”.

Now I need to compare the value of entry1 to the value of $entry.

If the values compare, then I would like to process the action described in the form element:

If the values do not compare, then I want to:

  • Delete the value entered for entry1
  • Alert the visitor that the answer wasn’t correct.
  • Return focus to the empty entry1 text box

How can I approach this?

Many Thanks,
Paul

There are millions of ways to make sure it is not a robot.

One super simple way is to just have an input field with a set value like this:

Then, when posted, just check for if(!isset($_POST["robot"])) If the entry for "robot" exists, it was not a human. If does not exist, continue processing.

Now, you can do it your way. Easy enough. One thing you must understand is that PHP is server-side only and user entries are client-side only. So, you can NOT use Javascript to handle this because any beginner programmer can alter the values. If you wanted to do it in your version, loosely do it this way. Not tested!
for the html:

<input name="number1" value="<?PHP echo rand(1,9); ?>" DISABLED> 
<input name="number2" value="<?PHP echo rand(1,9); ?>" DISABLED> 
<input name="number3" value=""> 

then for php
check if posted…

If( $_POST["number3]!=$_POST["number1"] + $_POST["number2"] ) {
    //  User did not enter correct value, display an error...
} else {
    //  User entered correct data, process rest of data here...
}

Not test, but, something like this should work for you. Remember == is the test if equal, != is not-equal
Good luck !

Sponsor our Newsletter | Privacy Policy | Terms of Service