首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何显示所选选项的结果

如何显示所选选项的结果
EN

Stack Overflow用户
提问于 2016-03-01 12:05:08
回答 3查看 71关注 0票数 0

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

代码语言:javascript
复制
<!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代码:

代码语言:javascript
复制
<?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";

?>

EN

回答 3

Stack Overflow用户

发布于 2016-03-01 12:35:36

根据你的选择进行转换。再试试这个。您可以按如下方式打印选定的选项值。

HTML和PHP:

代码语言:javascript
复制
<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
EN

Stack Overflow用户

发布于 2016-03-01 12:42:32

您可以尝试这样做:

html代码

代码语言:javascript
复制
<!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>

代码

代码语言:javascript
复制
<?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";

?>
票数 0
EN

Stack Overflow用户

发布于 2016-03-01 13:07:37

使用内爆我们可以显示所选择的内容

代码语言:javascript
复制
           $select_val=$_POST["b"];
           $comma_separated = implode(",", $select_val);
           echo  $comma_separated ;

array_sum将数组中的所有值相加,并显示总数

代码语言:javascript
复制
          echo "total  = " . array_sum($select_val) . "\n";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35714439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档