如果未选中所有复选框,则Php显示错误消息(多个复选框)

时间:2014-07-14 06:26:26

标签: php

在我的Html表单中,有几个用户可以选择的Checkbox字段现在,如果用户选择了所有Checkbox,则表单显示成功消息但是如果未选中所有复选框,则表示&#39 ; s显示几条错误消息。因此它没有显示成功消息。错误消息看起来像这样:

Notice: Undefined variable: mon_morning in D:\software installed\xampp installed\htdocs\tutor
\tutor_request.php on line 142

Notice: Undefined variable: mon_afternoon in D:\software installed\xampp installed\htdocs\tutor
\tutor_request.php on line 142

more.....

我的html复选框表格:

<table width="500" border="0" cellspacing="0" cellpadding="0">  
  <tr>
    <td><input type="checkbox" name="mon_morning" value="mon_morning" id="checkAll"/>&nbsp;<strong>MON</strong></td>
    <td><input type="checkbox" name="mon_morning" value="mon_morning" class="checkItem"/>&nbsp; Morning</td>
    <td><input type="checkbox" name="mon_afternoon" value="mon_morning" class="checkItem"/>&nbsp; Afternoon</td>
    <td><input type="checkbox" name="mon_evening" value="mon_evening" class="checkItem"/>&nbsp; Evening</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="tue_morning" value="tue_morning" id="checkAll_1"/>&nbsp;<strong>TUE</strong></td>
    <td><input type="checkbox" name="tue_morning" value="tue_morning" class="checkItem_1"/>&nbsp; Morning</td>
    <td><input type="checkbox" name="tue_afternoon" value="tue_morning" class="checkItem_1"/>&nbsp; Afternoon</td>
    <td><input type="checkbox" name="tue_evening" value="tue_evening" class="checkItem_1"/>&nbsp; Evening</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="wed_morning" value="wed_morning" id="checkAll_2"/>&nbsp;<strong>WED</strong></td>
    <td><input type="checkbox" name="wed_morning" value="wed_morning"/>&nbsp; Morning</td>
    <td><input type="checkbox" name="wed_afternoon" value="wed_morning"/>&nbsp; Afternoon</td>
    <td><input type="checkbox" name="wed_evening" value="wed_evening"/>&nbsp; Evening</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="thu_morning" value="thu_morning" id="checkAll_3"/> <strong>THU</strong></td>
    <td><input type="checkbox" name="thu_morning" value="thu_morning"/>&nbsp; Morning</td>
    <td><input type="checkbox" name="thu_afternoon" value="thu_morning"/>&nbsp; Afternoon</td>
    <td><input type="checkbox" name="thu_evening" value="thu_evening"/>&nbsp; Evening</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="fri_morning" value="fri_morning" id="checkAll_4"/> <strong>FRI</strong></td>
    <td><input type="checkbox" name="fri_morning" value="fri_morning"/>&nbsp; Morning</td>
    <td><input type="checkbox" name="fri_afternoon" value="fri_morning"/>&nbsp; Afternoon</td>
    <td><input type="checkbox" name="fri_evening" value="fri_evening"/>&nbsp; Evening</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="sat_morning" value="sat_morning" id="checkAll_5"/> <strong>SAT</strong></td>
    <td><input type="checkbox" name="sat_morning" value="sat_morning"/>&nbsp; Morning</td>
    <td><input type="checkbox" name="sat_afternoon" value="sat_morning"/>&nbsp; Afternoon</td>
    <td><input type="checkbox" name="sat_evening" value="sat_evening"/>&nbsp; Evening</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="sun_morning" value="sun_morning" id="checkAll_6"/> <strong>SUN</strong></td>
    <td><input type="checkbox" name="sun_morning" value="sun_morning"/>&nbsp; Morning</td>
    <td><input type="checkbox" name="sun_afternoon" value="sun_morning"/>&nbsp; Afternoon</td>
    <td><input type="checkbox" name="sun_evening" value="sun_evening"/>&nbsp; Evening</td>
  </tr>
</table>

Php构成了我处理Checkbox的方式:

if(isset($_POST['mon_morning'])) { $mon_morning = inputvalid($_POST['mon_morning']);}
if(isset($_POST['mon_afternoon'])) $mon_afternoon = inputvalid($_POST['mon_afternoon']);
if(isset($_POST['mon_evening'])) $mon_evening = inputvalid($_POST['mon_evening']); 
if(isset($_POST['tue_morning'])) $tue_morning = inputvalid($_POST['tue_morning']); 
// more.....

Javascript我用来检查一行内的所有Checkbox

<script>
$('td:first-child input[type="checkbox"]').on('change', function(){
    $(this)
    .closest('td')
    .siblings()
    .find('input[type="checkbox"]').prop('checked', this.checked);
});
</script>

2 个答案:

答案 0 :(得分:2)

未发布未选中的复选框。这就是您使用isset()进行检查,然后为复选框创建变量的原因。但是,如果未选中该复选框,则不会创建空变量。这就是为什么你得到一个变量未定义的通知。

尝试将您的PHP代码更改为

if(isset($_POST['mon_morning'])) {
    $mon_morning = inputvalid($_POST['mon_morning']);
} else {
    $mon_morning = false;
}

答案 1 :(得分:0)

Try this code:
if (isset($_POST['mon_morning']) && !empty($_POST['mon_morning'])) {
    $mon_morning = inputvalid($_POST['mon_morning']);
} else {
    $mon_morning = null;
}