我想检查用户是否选择了任何一个输入字段, 我试过了,
if(empty($_POST["month"]) and ($_POST["eid"]))
{
...
}
但是当我使用||时,条件是正确的当我使用&&&&&&& operator.How我能解决这个问题吗?
答案 0 :(得分:1)
基本上,声明如下:
if ($condition1 || $condition2) {}
在TRUE
或$condition1
为真时返回$condition2
。
并且,声明如下:
if ($condition1 && $condition2) {}
当TRUE
和$condition1
都为真时,返回$condition2
。
在您的情况下,您需要使用:
if(! empty($_POST["month"]) || ! empty($_POST["eid"])) {
// do something
}
答案 1 :(得分:0)
我想检查用户是否选择了任何一个输入字段
根据以上声明,您只需检查使用isset()
:
if(isset($_POST["month"]) || isset($_POST["eid"]))
答案 2 :(得分:0)
另一种检查方式:
用户是否填写了至少一个字段基本上是要求所有字段都是空的?如果不是 - 用户已经检查了至少一个字段,否则 - 没有。
所以只需使用&&运营商并添加!在整个条件下。
if(! ( empty($_POST['field1']) && empty($_POST['field2']) ... ) ) {
}
修改强> OP的条件:
我有三组filelds,例如date1和date2,id和date,month If 用户从这三个集合中选择任何内容我想显示一条消息 如果用户选择了他们可以进入的任何一个集合。
我希望我理解你是对的。如果没有,请分享一个例子。
代码:
if( (empty($_POST['date1']) and empty($_POST['date2'])) or
(empty($_POST['id']) and empty($_POST['date'])) or
(empty($_POST['month'])) ){
echo 'your message here.';
} else {
//the user can go in.
}
答案 3 :(得分:0)
您可以使用此功能检查空字段,而使用&&
时代码无效,因为($_POST["eid"])
没有规则。
示例:
//You check the month are empty and the eid are ... ?
if(empty($_POST["month"]) && ($_POST["eid"]))
//Use this if you want to check the "month" and "eid" are Empty
if(empty($_POST["month"]) && empty($_POST["eid"])) {
... your validation here ...
}
//You can you is_null too
if(is_null($_POST["month"]) && is_null($_POST["eid"])) {
... your validation here ...
}
如果您只想验证$_POST["month"]
或$_POST["eid"]
之间只有一个,只需将&&
更改为||
。
答案 4 :(得分:0)
一种简洁的方法是创建一个包含所有值的数组,然后检查它们是否为空。
If(empty(array($_POST["month"], $_POST["eid"]))){
// They are both empty
}else{
// Not empty
}