我有一个复选框和两个日期字段(从、到(。默认情况下,日期字段处于禁用状态,并在选中复选框时启用。若复选框被选中,那个么POST并没有问题,并且日期值是在发布页面中获得的。但当复选框未选中时,日期字段将被禁用,在发布时,我会收到以下错误。
Notice: Undefined index: from
Notice: Undefined index: To
Notice: Undefined index: checked
我该如何解决这个问题?
检查复选框是否选中。如果是这样,那么您可以使用其他发布的变量。如果不是,则不设置这些变量。
if(isset($_POST['checked'])) {
//do your stuff
} else {
//not checked do something else (from and to values don't exist)
}
这是因为未设置$_POST['from']
、$_POST['To']
和$_POST['checked']
。
您需要使用isset
函数来确保它存在。
用途:
if(isset($_POST['from']) && isset($_POST['To']) && isset($_POST['checked']) { // if all are required or your custom condition
// do stuff
} else {
// error handling
}