2017-06-15 40 views
-1

作爲一個長期的Java用戶,條件語句的想法對我來說並不新鮮。我最近開始學習php,並試圖從下拉列表中訪問數據並顯示它時遇到了困難。下面是HTML:有條件地跨越PHP和HTML

<!DOCTYPE html> 
<html> 
<head> 
    <title>Bob's Auto Parts - Place an Order</title> 
</head> 
<body> 
    <form action = "/processorder.php" method="post"> 
     <table style = "border: opx;"> 
     <tr style = "background: #cccccc;"> 
      <td style = "width: 150px; text-align: center;">Item</td> 
      <td style = "width: 15px; text-align: center;">Quantity</td> 
     </tr> 
     <tr> 
      <td>Tires</td> 
      <td><input type = "text" name = "tireqty" size = "3" maxlength = "3" /></td> 
     </tr>  
     <tr> 
      <td>Oil</td> 
      <td><input type = "text" name = "oilqty" size = "3" maxlength = "3" /></td> 
    </tr> 
    <tr> 
     <td>Spark Plugs</td> 
     <td><input type = "text" name = "sparkqty" size = "3" maxlength = "3" /></td> 
    </tr> 
    <tr> 
     <td>How did you find Bob's</td> 
     <td><select name = "find"> 
     <option value = "a">I'm a regular customer</option> 
     <option value = "b">TV advertising</option> 
     <option value = "c">Phone directory</option> 
     <option value = "d">Word of mouth</option> 
     </select> 
     </td> 
    </tr> 
    <tr> 
     <td colspan = "2" style = "text-align: center;"><input type = 
"submit" value = "Submit Order" /></td> 
     </tr>   
     </table> 
    </form>  
</body> 
</html> 

以及相應的PHP:

 <!DOCTYPE html> 
<html> 
<head> 
    <title>Bob's Auto Parts - Order Results</title> 
</head> 
<body> 
<h1>Bob's Auto Parts</h1> 
<h2>Order Results</h2> 
    <?php 
     echo "<p>Order processed at: "; 
     echo date('H:i, jS F Y'); 
     echo "</p>"; 

     // create short variable names 
     $tireqty = $_POST['tireqty']; 
     $oilqty = $_POST['oilqty']; 
     $sparkqty = $_POST['sparkqty']; 

     echo '<p>Your order is as follows: </p>'; 
     echo htmlspecialchars($tireqty). ' tires<br/>'; 
     echo htmlspecialchars($oilqty). ' bottles of oil<br/>'; 
     echo htmlspecialchars($sparkqty). ' spark plugs<br/>'; 

     /* is the same as... 

     echo '<p>Your order is as follows: </p>'; 
     echo htmlspecialchars($tireqty); echo ' tires<br/>'; 
     echo htmlspecialchars($oilqty); echo ' bottles of oil<br/>'; 
     echo htmlspecialchars($sparkqty); echo ' spark plugs<br/>'; 

     */ 

     $totalqty = 0; 
     $totalqty = $tireqty + $oilqty + $sparkqty; 
     echo "<p>Items ordered: ".$totalqty."<br />"; 
     $totalamount = 0; 

     define('TIREPRICE', 100); 
     define('OILPRICE', 10); 
     define('SPARKPRICE', 4); 

     $totalamount = $tireqty * TIREPRICE + 
         $oilqty * OILPRICE + 
         $sparkqty * SPARKPRICE; 

     echo "Subtotal: $".number_format($totalamount, 2)."<br />" ; 

     $taxrate = 0.10; // local sales tax is 10% 
     $totalamount = $totalamount * (1 + $taxrate); 
     echo "Total including tax: $".number_format($totalamount, 2)."</p>"; 
     echo '$find'; 

     /* good for making sure forms are filled out... 

     echo 'isset($tireqty): '.isset($tireqty).'<br />'; 
     echo 'isset($nothere): '.isset($nothere).'<br />'; 
     echo 'empty($tireqty): '.empty($tireqty).'<br />'; 
     echo 'empty($nothere): '.empty($nothere).'<br />'; 

     */ 


     if ($find == "a"){ 
       echo "<p>Regular Customer</p>"; 
     } 

     elseif ($find == "b"){ 
       echo "<p>Customer referred by TV advert</p>"; 
     } 

     elseif ($find == "c"){ 
       echo "<p>Customer referred by phone directory</p>"; 
     } 

     elseif ($find == "d"){ 
       echo "<p>Customer referred by word of mouth</p>"; 
     } 

     else { 
       echo "<p>We do not know how this customer found us</p>";    
     } 
    ?>  
</body> 
</html> 

簡單地說,在結果頁面始終顯示什麼是包含在「其他」的語句,即使其他選項被按下。任何意見/建議真的很感激。謝謝。

+1

'$ find'從哪裏來?你有你提交的表格嗎? – j08691

+1

這是整個代碼? – Swellar

+0

嘗試回顯'$ find',看看它的價值是什麼,如果它不是你的預期,你應該能夠找出原因。 –

回答

0

你可以參考這裏檢查你是否錯過了一些東西。我已經測試過了,它的工作原理如下。

<form action="" method="POST"> 
    <table> 
     <tr> 
      <td>How did you find Bob's</td> 
      <td> 
       <select name = "find"> 
        <option value = "a">I'm a regular customer</option> 
        <option value = "b">TV advertising</option> 
        <option value = "c">Phone directory</option> 
        <option value = "d">Word of mouth</option> 
       </select> 
      </td> 
     </tr> 
    </table> 
    <input type="submit" name="submit" value="Submit"/> 
</form> 
<?php 
    if (isset($_POST['submit'])) { 
     $find = $_POST['find']; 
     if ($find == "a"){ 
      echo "<p>Regular Customer</p>"; 
     } 

     elseif ($find == "b"){ 
       echo "<p>Customer referred by TV advert</p>"; 
     } 

     elseif ($find == "c"){ 
       echo "<p>Customer referred by phone directory</p>"; 
     } 

     elseif ($find == "d"){ 
       echo "<p>Customer referred by word of mouth</p>"; 
     } 

     else { 
       echo "<p>We do not know how this customer found us</p>";    
     } 
    } 
?>