2016-03-01 21 views
0

我有這種形式會增加所選項目的總成本,但我需要顯示結果中選擇的內容。如何在結果中顯示選定的選項? 我需要在表格中顯示買家的訂購內容。如何顯示選定的選項結果

<!DOCTYPE html> 
 
<head> 
 
    <title> Light Bulbs Sales Form </title> 
 
</head> 
 

 
<body> 
 
    <h2> Welcome to Light Bulbs Sales Form </h2> 
 
    <form action="index.php" method="post"> 
 
     <p> 
 
      <label> Buyer's Name: 
 
       <input type="text" name="name" size="30" /> 
 
      </label> 
 
     <p/> 
 
     <table> 
 
      <tr> 
 
       <th></th> 
 
       <th>Product Name</th> 
 
       <th>Price</th> 
 
      </tr> 
 
      <tr> 
 
       <td><input type="checkbox" name="b[]" value="2.39"/></td> 
 
       <td> Four 25-watt light bulbs </td> 
 
       <td> $2.39 </td>      
 
      </tr> 
 
      <tr> 
 
       <td><input type="checkbox" name="b[]" value="4.29"/></td> 
 
       <td> Eight 25-watt light bulbs </td> 
 
       <td> $4.29 </td>      
 
      </tr> 
 
      <tr> 
 
       <td><input type="checkbox" name="b[]" value="3.95"/></td> 
 
       <td> Four 25-watt long-life light bulbs </td> 
 
       <td> $3.95 </td> 
 
      </tr> 
 
      <tr> 
 
       <td><input type="checkbox" name="b[]" value="7.49"/></td> 
 
       <td> Eight 25-watt long-life light bulbs </td> 
 
       <td> $7.49 </td>  
 
      </tr> 
 
     </table> 
 
     <h3>Payment Method</h3> 
 
      <p> 
 
       <label><input type="radio" name="payment" value="Visa"/>Visa</label> 
 
       <br/> 
 
       <label><input type="radio" name="payment" value="Master Card"/>Master Card</label> 
 
       <br/> 
 
       <label><input type="radio" name="payment" value="Discover"/>Discover</label> 
 
       <br/> 
 
      </p> 
 
      <p> 
 
       <input type="submit" value="Submit Order"/> 
 
       <input type="reset" value="Clear Order Form"/> 
 
      </p> 
 
     </form> 
 
</body> 
 
</html>

這是我的PHP代碼:

<?php 
 
$name=$_POST["name"]; 
 
$bulbs=$_POST["b"]; 
 
$pmode=$_POST["payment"]; 
 

 
$total=0; 
 
for($i=0;$i<sizeof($bulbs);$i++) 
 
{ 
 
    $tax=$bulbs[$i]*0.062; 
 
    $total=$total+$bulbs[$i]+$tax; 
 
    $totalcost=number_format((float)$total, 2, '.', ''); 
 
} 
 

 
print "Your name is: $name <br>"; 
 
print "Your total cost is: $totalcost <br>"; 
 
print "Your chosen method of payment is: $pmode"; 
 

 
?>

+0

預期什麼不工作? – Rayon

回答

0

根據您的一個轉換。並嘗試這一個。您可以按如下所示打印選定的選項值。

HTML & PHP:

<form action="php_checkbox.php" method="post"> 
<label class="heading">Select One:</label> 
<input type="checkbox" name="check_list[]" value="4.29"><label>4.29</label> 
<input type="checkbox" name="check_list[]" value="3.25"><label>3.25</label> 
</form> 
<?php 


if(isset($_POST['submit'])){ 

if(!empty($_POST['check_list'])) { 

$checked_count = count($_POST['check_list']); 
echo "Selected values ".$checked_count." option(s): <br/>"; 

foreach($_POST['check_list'] as $selected) { 
echo "<p>".$selected ."</p>"; 

} 
else{ 
echo "<b>Please Select Atleast One Option.</b>"; 
} 
} 
?> 
0

你可以試試這個:

HTML代碼

<!DOCTYPE html> 
<head> 
    <title> Light Bulbs Sales Form </title> 
</head> 

<body> 
    <h2> Welcome to Light Bulbs Sales Form </h2> 
    <form action="action.php" method="post"> 
     <p> 
      <label> Buyer's Name: 
       <input type="text" name="name" size="30" /> 
      </label> 
     <p/> 
     <table> 
      <tr> 
       <th></th> 
       <th>Product Name</th> 
       <th>Price</th> 
      </tr> 
      <tr> 
       <td><input type="checkbox" name="b[]" value="2.39"/></td> 
       <td> Four 25-watt light bulbs </td> 
       <td> $2.39 </td>      
      </tr> 
      <tr> 
       <td><input type="checkbox" name="b[]" value="4.29"/></td> 
       <td> Eight 25-watt light bulbs </td> 
       <td> $4.29 </td>      
      </tr> 
      <tr> 
       <td><input type="checkbox" name="b[]" value="3.95"/></td> 
       <td> Four 25-watt long-life light bulbs </td> 
       <td> $3.95 </td> 
      </tr> 
      <tr> 
       <td><input type="checkbox" name="b[]" value="7.49"/></td> 
       <td> Eight 25-watt long-life light bulbs </td> 
       <td> $7.49 </td>  
      </tr> 
     </table> 
     <h3>Payment Method</h3> 
      <p> 
       <label><input type="radio" name="payment" value="Visa"/>Visa</label> 
       <br/> 
       <label><input type="radio" name="payment" value="Master Card"/>Master Card</label> 
       <br/> 
       <label><input type="radio" name="payment" value="Discover"/>Discover</label> 
       <br/> 
      </p> 
      <p> 
       <input type="submit" value="Submit Order"/> 
       <input type="reset" value="Clear Order Form"/> 
      </p> 
     </form> 
</body> 
</html> 

PHP代碼

<?php 
$name=$_POST["name"]; 
$bulbs=$_POST["b"]; 
$pmode=$_POST["payment"]; 
$select_val=$_POST["b"]; 
$total=0; 
for($i=0;$i<sizeof($bulbs);$i++) 
{ 
    $tax=$bulbs[$i]*0.062; 
    $total=$total+$bulbs[$i]+$tax; 
    $totalcost=number_format((float)$total, 2, '.', ''); 
} 

echo "Selected Product value:<br>"; 
for ($j=0; $j <count($select_val) ; $j++) { 
    echo ($j+1).")".$select_val[$j]."<br>"; 
} 

print "Your name is: $name <br>"; 
print "Your total cost is: $totalcost <br>"; 
print "Your chosen method of payment is: $pmode"; 

?> 
012使用內爆,我們可以展示一下已被選中

  $select_val=$_POST["b"]; 
      $comma_separated = implode(",", $select_val); 
      echo $comma_separated ; 

array_sum增加了陣列中的所有值並顯示

+0

謝謝你的回答,但在這段代碼顯示選定的物品價格,但我需要顯示選定的項目本身我該怎麼做? – SQATube

0

  echo "total = " . array_sum($select_val) . "\n"; 
相關問題