如何显示所选的选项结果

时间:2016-03-01 04:05:08

标签: php

我有这个表单将添加所选项目的总成本,但我需要显示在结果中选择的内容。如何在结果中显示所选选项? 我需要在表格中显示买家的确切订单。

<!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";

?>

3 个答案:

答案 0 :(得分:0)

根据你的转换。试试这个。您可以按如下方式打印所选的选项值。

HTML&amp; 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>";
}
}
?>

答案 1 :(得分: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";

?>

答案 2 :(得分:0)

使用implode我们可以显示已选择的内容

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

array_sum添加数组中的所有值并显示总数

          echo "total  = " . array_sum($select_val) . "\n";