php表单处理多个复选框

时间:2014-04-17 15:41:23

标签: php forms if-statement checkbox

我是PHP表单处理的新手,除了使用Checkboxes处理PHP之外,我已经很好地处理了它。

我遇到的问题是我似乎无法从我的问题中回答出正确的3个答案。我会在我的测验中完成表格,勾选正确的三个复选框,由于某种原因PHP输出说我输错了答案!希望你能提供帮助,任何答案都将不胜感激! :)

这是我的表格和问题:

    <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 />

这是我的PHP,我确定这是完全错误的,但希望你知道我想要实现的目标:

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."; 
}

&GT;

2 个答案:

答案 0 :(得分:2)

尝试

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

然后$golfer将是PHP中的数组,您可以与正确的值进行比较

以下PHP代码将检查提交的答案是否为'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.";
}

答案 1 :(得分:0)

将复选框设为数组

<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 />

表单提交后

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