2017-06-15 7 views
-1

長年のJavaユーザーとして、条件文の考え方は私には新しくない。私は最近、PHPを学び始め、ドロップダウンリストからデータにアクセスして表示しようとすると難しかったです。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> 

は、簡単に言えば、結果ページには、常に「他」の文に含まれているものを表示する別のオプションが押された場合にも、ここでは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>";    
     } 
    } 
?>