PHP检查至少选择了两个按钮


PHP check atleast two buttons are selected

我有一个表单,需要用户选择/单击至少两个按钮才能提交表单

<button type="button" name="Investor-agree-one">I AGREE</button>
<button type="button" name="Investor-agree-two">I AGREE</button>
<button type="button" name="Investor-agree-three">I AGREE</button>
<button type="button" name="Investor-agree-four">I AGREE</button>
<button type="button" name="Investor-agree-five">I AGREE</button>

我如何使用php验证表单,即至少选择了两个按钮,并将用户重定向到一个页面,如果没有重定向到另一个页面?所以基本上是这样的:

if(buttonSelected>=2){
    goto this page
}else{
    goto another page
    }

如何使用按钮元素指示是否首先选择了按钮?

这很容易,

为所有按钮指定相同的"名称"和唯一值假设我们有这个按钮标签:

<form method="post">
<button name="somebutton" value="buttonone">
<button name="somebutton" value="buttontwo>
<button name="somebutton" value="buttontwo">
</form>

然后,您的php应该看起来像这样:

<?php
$button = $_POST['somebutton'];
if($button == "buttonone"){
    //do button 1 stuff, in your example:
    header('location: someurl.php');
}
if($button == "buttontwo"){
    // do button 2 stuff
}
?>

您可以使用复选框而不是按钮,因此您的代码可能如下所示:

<?php
    if(isset($_POST['agree_one'])) {
        // do something
    }
?>
<form method="post">
    <label>
        <input type="checkbox" name="agree_one" value="1"/>
        I Agree
    </label>
    <label>
        <input type="checkbox" name="agree_two" value="1"/>
        I Agree
    </label>
    <label>
        <input type="checkbox" name="agree_three" value="1"/>
        I Agree
    </label>
</form>

但如果你只想计算有多少用户选择了同意复选框,你可能需要这个代码:

<?php
if(isset($_POST['agree']) && count($_POST['agree']) > 2) {
    // do magic
}
?>
<form method="post">
    <label>
        <input type="checkbox" name="agree[]" value="1"/>
        I Agree
    </label>
    <label>
        <input type="checkbox" name="agree[]" value="1"/>
        I Agree
    </label>
    <label>
        <input type="checkbox" name="agree[]" value="1"/>
        I Agree
    </label>
</form>