0

I'm trying to create a simple PHP checkbox validator for my boss's website but my PHP code doesn't work. I've jimmied some basic checkbox code I found on the net, and tried working with an "if" statement but I get the same result whether I check the box or not.

Ideally I'd like to set it up where you have to check the box to prove you're not a spambot, not uncheck it. I'll settle for either solution though. How do I fix my code?

Live form: http://agentboris.com/listings/test-form.php

HTML:

<input type="checkbox" id="spambot" name="spambot" value="Yes" checked>
&nbsp;&nbsp;&nbsp;<label for='spambot'><span class="parastyle grey">Uncheck box to show you are not a Spambot.</span></label><br><br />

PHP (condensed):

$spambot = $_POST['spambot'];

if ($spambot != 'Yes') {
    $spambot = 'No';
} 

if($spambot = 'Yes')
{
    echo "Please go back and uncheck the Spambot box.";
    exit;
}
Gary
  • 13,303
  • 18
  • 49
  • 71
Alex Banman
  • 526
  • 1
  • 6
  • 20
  • 3
    `=` != `==` in your second `if` – cmorrissey Jul 22 '15 at 16:03
  • 2
    `if($spambot = 'Yes')` is an assignment. Use `if($spambot == 'Yes')` for comparison – andrewsi Jul 22 '15 at 16:03
  • @andrewsi. if you want to make that an answer i'll mark it as correct, otherwise thanks, it worked. – Alex Banman Jul 22 '15 at 16:07
  • @Alex Banman you always get de message "Please go back and uncheck the Spambot box." because `$spambot = 'Yes'` returns an String"Yes". So `if($spambot = 'Yes')` that will execute as `if("Yes")` and that statement is always true. PHP should not allow an assignment within an if statement – Raymond Nijland Jul 22 '15 at 16:38
  • @RaymondNijland thanks for the clarification, it seems i need to familiarize myself with php comparisons – Alex Banman Jul 22 '15 at 16:41

0 Answers0