Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php限制所需的复选框数_Php - Fatal编程技术网

使用php限制所需的复选框数

使用php限制所需的复选框数,php,Php,我三天来一直在寻找解决办法。希望有人能帮忙。复选框需要将用户限制为2个或更多选择。我知道如何使用javascript实现这一点,但需要使用PHP。这可能吗?如果可能,如何实现 <label><input type="checkbox" value="Lemon" name='filling[]' />Lemon</label></br> <label><input type="checkb

我三天来一直在寻找解决办法。希望有人能帮忙。复选框需要将用户限制为2个或更多选择。我知道如何使用javascript实现这一点,但需要使用PHP。这可能吗?如果可能,如何实现

            <label><input type="checkbox" value="Lemon" name='filling[]'  />Lemon</label></br>
        <label><input type="checkbox" value="Custard" name='filling[]'  />Custard</label></br>
        <label><input type="checkbox" value="Fudge" name='filling[]'  />Fudge</label></br>
        <label><input type="checkbox" value="Mocha" name='filling[]'  />Mocha</label></br>
        <label><input type="checkbox" value="Raspberry" name='filling[]'  />Raspberry</label></br>
        <label><input type="checkbox" value="Pineapple" name='filling[]'  />Pineapple</label>

您可以通过计算选择的数量来实现这一点

if(count($_POST['filling']) < 2){
    // throw error
}

我假设您试图通过PHP处理表单提交,并希望用户选择至少2个复选框。提交表单时,只有选中复选框,才会将其发送到服务器。在设置中,使用数组作为名称。只需检查数组的大小是否至少为2

$checkboxes = $_POST['filling'];

if( count($checkboxes) < 2 ) {
    die("You must select at least 2 checkboxes.");
}

显然,你可以做更多的事情,但你明白了要点。

这两个回答给了我一个好的开始。让它与下面的php一起工作

if(empty($_POST['filling']) || count($_POST['filling']) < 2)
{
show_error("Please select at least two fillings");
}
$filling = check_input(implode("," , $_POST['filling']));

php是一种服务器端语言。它无法监视/限制某人在浏览器中的行为。最多可以知道表单提交到服务器后检查了多少复选框。对于浏览器中的这些限制,您需要JavaScript,在计算输入的填充时可能会抛出PHP错误,但这将是荒谬的验证和错误。为什么需要使用PHP?