如何使用PHP验证输入框的多个复选框?

时间:2017-01-22 10:57:03

标签: php

我有以下12个复选框和输入框。我使用名为ch_name[]ch_for[]

的数组获取复选框和输入框值
<div class="checkbox form-inline">
    <label><input type="checkbox" name="ch_name[]" value="ch1">CH01</label>
    <input type="text" name="ch_for[]" value="" placeholder="Channel details" class="form-control ch_for">
</div>

现在我调用Ajax请求验证是否至少有一个复选框为空,并且对应的输入框为空。

因此,我使用以下PHP代码,但不知何故,它不起作用。可能是我遗失了一些东西:(

Php验证:

$ch_name        = isset($_POST['ch_name']) ? $_POST['ch_name'] : '';
$ch_for         = isset($_POST['ch_for']) ? $_POST['ch_for'] : '';  

if( count($ch_name) > 0  ) {        
    if ( isset($ch_name, $ch_for) && !empty($ch_name) && !empty($ch_for) ) {
        foreach ( $ch_name as $key => $ch_name) {
            if( empty($_POST['ch_name']) ) {
                $errors[] = 'Select your channel name';
            }

            if( empty($ch_for[$key]) ) {
                $errors[] = "Enter your channel details";
            }                   
        }
    }
}

0 个答案:

没有答案