doufen3563 2014-04-17 07:41
浏览 56
已采纳

php表单处理多个复选框

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条回答 默认 最新

  • doupo2633 2014-04-17 07:42
    关注

    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.";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部