Php If 语句带有 &&运算符不起作用(逻辑与)

发布于 2024-11-14 11:51:50 字数 1668 浏览 2 评论 0原文

代码如下,但是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回首观望 2024-11-21 11:51:50

如果你真的想使用 &&而不是 ||,您的选择表单需要是:

<select name = "when">
                    <option value="" selected="selected"> </option>
                    <option value="breakfast">Breakfast</option>
                    <option value="lunch">Lunch</option>
                    <option value="dinner">Dinner</option>
                    <option value="snacks">Snacks</option>
</select> 

您没有传递默认所选项目的空值和 && if clouse 不会评估为 true。

编辑:再想一想,您的表单必须是这样的,否则默认的空值将不会被捕获。无论您决定使用 &&或||

If you really meant to use && and not ||, your select form needs to be:

<select name = "when">
                    <option value="" selected="selected"> </option>
                    <option value="breakfast">Breakfast</option>
                    <option value="lunch">Lunch</option>
                    <option value="dinner">Dinner</option>
                    <option value="snacks">Snacks</option>
</select> 

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 ||

浮生未歇 2024-11-21 11:51:50

我建议在您的情况下使用 || 而不是 && ,因为您想检查它们是否为空,而不是两者都为空:

            if(($_POST['term'] == "") ||
                ($_POST['when'] == "")) {
                echo "Please Fill The Fields Left to Right";
                echo "<br/>";
            }

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:

            if(($_POST['term'] == "") ||
                ($_POST['when'] == "")) {
                echo "Please Fill The Fields Left to Right";
                echo "<br/>";
            }
君勿笑 2024-11-21 11:51:50

另外,请考虑使用 !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 {

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文