如果未选中“全部”复选框,则显示错误消息的Php(多个复选框)
在我的Html表单中,用户可以选择或不选择的复选框字段很少。现在,如果用户选中所有复选框,则表单显示成功消息,但如果未选中所有复选框,则显示多条错误消息。所以它没有显示成功的信息。错误消息如下所示:如果未选中“全部”复选框,则显示错误消息的Php(多个复选框),php,Php,在我的Html表单中,用户可以选择或不选择的复选框字段很少。现在,如果用户选中所有复选框,则表单显示成功消息,但如果未选中所有复选框,则显示多条错误消息。所以它没有显示成功的信息。错误消息如下所示: Notice: Undefined variable: mon_morning in D:\software installed\xampp installed\htdocs\tutor \tutor_request.php on line 142 Notice: Undefined variab
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"/> <strong>MON</strong></td>
<td><input type="checkbox" name="mon_morning" value="mon_morning" class="checkItem"/> Morning</td>
<td><input type="checkbox" name="mon_afternoon" value="mon_morning" class="checkItem"/> Afternoon</td>
<td><input type="checkbox" name="mon_evening" value="mon_evening" class="checkItem"/> Evening</td>
</tr>
<tr>
<td><input type="checkbox" name="tue_morning" value="tue_morning" id="checkAll_1"/> <strong>TUE</strong></td>
<td><input type="checkbox" name="tue_morning" value="tue_morning" class="checkItem_1"/> Morning</td>
<td><input type="checkbox" name="tue_afternoon" value="tue_morning" class="checkItem_1"/> Afternoon</td>
<td><input type="checkbox" name="tue_evening" value="tue_evening" class="checkItem_1"/> Evening</td>
</tr>
<tr>
<td><input type="checkbox" name="wed_morning" value="wed_morning" id="checkAll_2"/> <strong>WED</strong></td>
<td><input type="checkbox" name="wed_morning" value="wed_morning"/> Morning</td>
<td><input type="checkbox" name="wed_afternoon" value="wed_morning"/> Afternoon</td>
<td><input type="checkbox" name="wed_evening" value="wed_evening"/> 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"/> Morning</td>
<td><input type="checkbox" name="thu_afternoon" value="thu_morning"/> Afternoon</td>
<td><input type="checkbox" name="thu_evening" value="thu_evening"/> 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"/> Morning</td>
<td><input type="checkbox" name="fri_afternoon" value="fri_morning"/> Afternoon</td>
<td><input type="checkbox" name="fri_evening" value="fri_evening"/> 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"/> Morning</td>
<td><input type="checkbox" name="sat_afternoon" value="sat_morning"/> Afternoon</td>
<td><input type="checkbox" name="sat_evening" value="sat_evening"/> 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"/> Morning</td>
<td><input type="checkbox" name="sun_afternoon" value="sun_morning"/> Afternoon</td>
<td><input type="checkbox" name="sun_evening" value="sun_evening"/> Evening</td>
</tr>
</table>
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
<script>
$('td:first-child input[type="checkbox"]').on('change', function(){
$(this)
.closest('td')
.siblings()
.find('input[type="checkbox"]').prop('checked', this.checked);
});
</script>
$('td:first child input[type=“checkbox”]”)。在('change',function()上{
$(本)
.最近的('td')
.兄弟姐妹()
.find('input[type=“checkbox”]').prop('checked',this.checked);
});
未选中的复选框不会过帐。这就是为什么要用isset()
选中此复选框,然后为复选框创建变量。但如果未选中复选框,则不会创建空变量。这就是为什么会得到一个变量未定义的通知
尝试将php代码更改为
if(isset($_POST['mon_morning'])) {
$mon_morning = inputvalid($_POST['mon_morning']);
} else {
$mon_morning = false;
}
什么是表单方法GET/POST?
isset
在这里不是一个很好的选项,因为如果提交了复选框但没有设置值,它也会返回falsearray\u key\u exists
是一个更好、更可靠的选择。
Try this code:
if (isset($_POST['mon_morning']) && !empty($_POST['mon_morning'])) {
$mon_morning = inputvalid($_POST['mon_morning']);
} else {
$mon_morning = null;
}