if和else with和

时间:2013-08-08 06:32:11

标签: php mysql if-statement

我的第一页是复选框表格

    <!DOCTYPE html>
<html>
<body>

<form action="1.php" method="POST">
<input type="checkbox" name="count" value="count">count<br>
<input type="checkbox" name="search" value="search">search<br>
<input type="checkbox" name="order" value="order">order 
<input type="submit" value ="submit">
</form>

</body>
</html>

然后我的下一页是使用post收集值,并使用if条件显示结果

  <?php
    if(empty($_POST['count'])) { 
           $_POST['count']=''; 
        }
        else { 
            $a=$_POST['count']; 
        }
        if(empty($_POST['order'])) {
            $_POST['order']=''; 
        }
        else { 
            $b=$_POST['order']; 
        }
         if(empty($_POST['search'])) {
            $_POST['search']=''; 
        }
        else { 
            $c=$_POST['search']; 
        }

       if($_POST['count']=="count" && $_POST['search']=="search" && $_POST['order']=="order" )  {
          echo  "I have a count ,search and order" ;
       } 
        else if($_POST['order']=="order" && $_POST['count']=="count" && $_POST['search']!="search"  )  {
           echo "I have a order and count";
        }
        else if($_POST['search']=="search" && $_POST['count']=="count" && $_POST['order']!="order"  )  {
           echo "I have a search and count";
        }
         else if($_POST['search']=="search" && $_POST['order']=="order" && $_POST['count']!="count"  )  {
           echo "I have a search and order";
        }           
       else if($_POST['count']=="count" || $_POST['search']=="search" || $_POST['order']=="order"  ) {
          echo $_POST['count'];
          echo $_POST['search'];
          echo $_POST['order'];
       }
?>

我的自行车状况正在发挥作用

我的汽车状况正在发挥作用

我有一辆车和自行车不工作,我是这种语言的新手所以请帮助

4 个答案:

答案 0 :(得分:3)

你试试:

...
if($_POST['vehicle']=="Bike" && $_POST['vehicle1']=="Car")  {
       echo  "I have a car and bike" ;
} else if($_POST['vehicle']=="Bike") {
        echo "I have a bike";
} else if($_POST['vehicle1']=="Car") {
       echo  "I have a car" ;
}

“汽车和自行车”无效,因为条件1为真=&gt;条件2和3不能再次测试。

IF - 否则if:如果条件1为假,则检查条件2,如果条件2为假,则检查条件3。

答案 1 :(得分:1)

删除else并尝试

<?php

if ($_POST['vehicle'] == "Bike" && $_POST['vehicle1'] == "Car")  {
   echo "I have a car and bike" ;
} else if ($_POST['vehicle'] == "Bike") {
    echo "I have a bike";
} else if ($_POST['vehicle1'] == "Car") {
   echo "I have a car" ;
}

?>    

答案 2 :(得分:0)

    if(empty($_POST['vehicle'])) { 
       $_POST['vehicle']=''; 
    }
    else { 
        $a=$_POST['vehicle']; 
    }
    if(empty($_POST['vehicle1'])) {
        $_POST['vehicle1']=''; 
    }
    else { 
        $b=$_POST['vehicle1']; 
    }
    if($_POST['vehicle']=="Bike" && $_POST['vehicle1']!="Car") {
       echo "I have a bike";
    }       
   elseif($_POST['vehicle1']=="Car" && $_POST['vehicle']!="Bike") {
      echo  "I have a car" ;
   }
   if($_POST['vehicle']=="Bike" && $_POST['vehicle1']=="Car")  {
      echo  "I have a car and bike" ;
   } 

答案 3 :(得分:0)

您的代码将更新到达上一个elseif语句。我会把最后一个if -statement移到顶部。

    <?
    if(empty($_POST['vehicle'])) { 
           $_POST['vehicle']=''; 
        }
        else { 
            $a=$_POST['vehicle']; 
        }
        if(empty($_POST['vehicle1'])) {
            $_POST['vehicle1']=''; 
        }
        else { 
            $b=$_POST['vehicle1']; 
        }
       if($_POST['vehicle']=="Bike" && $_POST['vehicle1']=="Car")  {
          echo  "I have a car and bike" ;
       } 
        else if($_POST['vehicle']=="Bike") {
           echo "I have a bike";
        }       
       else if($_POST['vehicle1']=="Car") {
          echo  "I have a car" ;
       }
?>