0

I'm new to PHP form handling and I've handled it pretty well- apart from dealing PHP with Checkboxes.

The problem I have is I can't seem to echo out the correct 3 answers from my question. I would complete the form on my quiz ticking the correct three check boxes and for some reason PHP output's saying I have entered the wrong answer! Hope you can help, any answers will be much appreciated! :)

Here is my form and my question:

    <p> 22. What professional golfers have won the masters?</p>

<input type="checkbox" name="golfer" value="a">Tiger Woods<br />
<input type="checkbox" name="golfer" value="b">Phil Mickelson<br />
<input type="checkbox" name="golfer" value="c">Lee Westwood<br />
<input type="checkbox" name="golfer" value="d">Bubba watson<br />

And here is my PHP, I'm sure it is completely wrong but hopefully you know what I'm trying to achieve:

if ($golfer == "a" && "b" && "d") {
    echo "That's the right answer, Tiger Woods, Phil Mickelson and Bubba Watson have all won the Masters.";
} else {
    echo "That's the wrong answer. Lee Westwood has not won a major."; 
}

?>

2 Answers 2

2

Try

<input type="checkbox" name="golfer[]" ...

Then $golfer will be an array in PHP and you can compare with the correct values

The following PHP code will check if the submitted answer is 'a', 'b', 'd'

sort($golfer);
$correct = array('a', 'b', 'd');

if ($golfer== $correct) {
    echo "That's the right answer, Tiger Woods, Phil Mickelson and Bubba Watson have all won the Masters.";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Never had these problems with Perl back in ye olde days when all yer intarwebs was powered by steam!
Still confused, sorry guys I'm very new to this! I have so far: foreach($golfer as $g) {}.... What do I do now?
I updated the answer, to give you a starting point about validating the different answers
0

Make check boxes as an array

<input type="checkbox" name="golfer[]" value="a">Tiger Woods<br /> 
<input type="checkbox" name="golfer[]" value="b">Phil Mickelson<br />
<input type="checkbox" name="golfer[]" value="c">Lee Westwood<br />
<input type="checkbox" name="golfer[]" value="d">Bubba watson<br />

After form submission

// The array will only contain the checked value of forms
foreach($golfer as $g) {
   // can use checkbox value here
}

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.