Php If 语句带有 &&运算符不起作用(逻辑与)
代码如下,但是if &&声明不起作用。我正在测试下拉列表和搜索框同时为空,但是当我从下拉列表中选择某些内容时,它会执行代码,或者我填写搜索框,否则如果两者都为空,则它可以工作。
<form action="post.php" method="POST">
<select name = "when">
<option selected= ""> </option>
<option value="breakfast">Breakfast</option>
<option value="lunch">Lunch</option>
<option value="dinner">Dinner</option>
<option value="snacks">Snacks</option>
</select>
<input type="text" name="term" value="" id="search">
<input type="submit" name="submit" value="Submit">
<div id="results"></div>
</form>
<?php
$term = $_POST['term'];
$when = $_POST['when'];
if(($_POST['term'] == "") &&
($_POST['when'] == "")) {
echo "Please Fill The Fields Left to Right";
echo "<br/>";
} else {
$foods = mysql_query("SELECT * FROM foods WHERE food_name LIKE '%$term%' ") or die(mysql_error());
while($food = mysql_fetch_array($foods)) {
$name = $food['food_name'];
$foodid = $food['food_ID'];
echo "<div class='results'><a href='http://localhost:8888/food/interact.php?add=$name'>" . $name . "</a></div>";
}
}
?>
The code is as follows but the if && statement doesn't work. I am testing for both the drop-down and search box to be empty at the same-time yet when I select something from the drop down it executes the code or I fill out the search box otherwise if both blank it works.
<form action="post.php" method="POST">
<select name = "when">
<option selected= ""> </option>
<option value="breakfast">Breakfast</option>
<option value="lunch">Lunch</option>
<option value="dinner">Dinner</option>
<option value="snacks">Snacks</option>
</select>
<input type="text" name="term" value="" id="search">
<input type="submit" name="submit" value="Submit">
<div id="results"></div>
</form>
<?php
$term = $_POST['term'];
$when = $_POST['when'];
if(($_POST['term'] == "") &&
($_POST['when'] == "")) {
echo "Please Fill The Fields Left to Right";
echo "<br/>";
} else {
$foods = mysql_query("SELECT * FROM foods WHERE food_name LIKE '%$term%' ") or die(mysql_error());
while($food = mysql_fetch_array($foods)) {
$name = $food['food_name'];
$foodid = $food['food_ID'];
echo "<div class='results'><a href='http://localhost:8888/food/interact.php?add=$name'>" . $name . "</a></div>";
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你真的想使用 &&而不是 ||,您的选择表单需要是:
您没有传递默认所选项目的空值和
&& if
clouse 不会评估为 true。编辑:再想一想,您的表单必须是这样的,否则默认的空值将不会被捕获。无论您决定使用 &&或||
If you really meant to use && and not ||, your select form needs to be:
You're not passing the empty value of the default selected item and the
&& if
clouse won't valuate as true.Edit: on a second thought, your form HAS TO BE like this, or the default empty value won't be catched. Wheter you decide to use && or ||
我建议在您的情况下使用
||
而不是&&
,因为您想检查它们是否为空,而不是两者都为空:I would suggest using an
||
instead of the&&
in your case, since you want check if either of them are empty, not if both of them are empty:另外,请考虑使用 !isset,这意味着未设置该变量。
if(!isset($_POST['term']) || $_POST['term'] == "" || !isset($_POST['when']) || $_POST['when'] == “”){
echo "请从左到右填写字段";
回声“
”;
} 别的 {
Also, consider using !isset, which means that the variable is not set.
if(!isset($_POST['term']) || $_POST['term'] == "" || !isset($_POST['when']) || $_POST['when'] == "") {
echo "Please Fill The Fields Left to Right";
echo "
";
} else {