开发者

Php If statement with && operator doesnt work (Logical And)

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 =开发者_JS百科 $_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>";

                }
            }


?>


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


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/>";
            }


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 {

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜