PHP复选框中元素的总和

PHP复选框中元素的总和,php,checkbox,Php,Checkbox,输出选中复选框的值的最佳方法是什么,但我想得到一个总数。我曾尝试使用sum()对所选的值求和,但我一直得到双倍的0 mycheckbox.php <?php if(isset($_POST['submit'])){ if(!empty($_POST['shipping'])) { ?> <div class="container"> <?php //Loop through shipping array to fetch individual shipp

输出选中复选框的值的最佳方法是什么,但我想得到一个总数。我曾尝试使用sum()对所选的值求和,但我一直得到双倍的0

mycheckbox.php

<?php
 if(isset($_POST['submit'])){
 if(!empty($_POST['shipping'])) {

 ?>
 <div class="container"> <?php
 //Loop through shipping array to fetch individual shipping so that we     can use them
 echo "<h2> Your Shipping Costs are: </h2>";
 foreach($_POST['shipping'] as $shipping) {
 //Print all the shipping
  echo $shipping;
  }

  }
  else{
  echo "<b>Please Select at least One Option.</b>";
  }
  ?></div><?php }
   ?>

然后我的html

 <div class="container">
    <form action="checkbox.php" method="post">

       <label class="head">Select Your Timezone or Other Shipping  Options:</label><br/><br/>
        <input type="checkbox" name="shipping[]" value="$190.00"> <label>Freight to Eastern USA</label><br/>
       <input type="checkbox" name="shipping[]" value="$245.00"><label>Freight to Western USA</label><br/>
       <input type="checkbox" name="shipping[]" value="$75.00"><label>Residential Devivery</label><br/>
       <input type="checkbox" name="shipping[]" value="$100.00"><label>Liftgate Service</label><br/>
       <input type="checkbox" name="shipping[]" value="$0.00"><label>Will Call</label><br/><br/>
       <input type="submit" name="submit" Value="Add to Shopping Cart"/>

    </form>

选择您的时区或其他配送选项:

到美国东部的运费
到美国西部的运费
住宅设备安装
提升门服务
将调用

非常感谢大家的帮助

PHP复选框中元素的总和

您需要删除美元符号(
$
),以便
对值求和。
使用类似于:

foreach($_POST['shipping'] as $shipping) {
    $shipping = str_replace("\$", "", $shipping);
    $sum +=  $shipping;
  }
    echo "\$".$sum;

在添加值之前,必须删除美元符号:

<?php
if(isset($_POST['submit'])){
    if(!empty($_POST['shipping'])) {
        echo('<div class="container">');
        //Loop through shipping array to fetch individual shipping so that we can use them
        echo "<h2> Your Shipping Costs are: </h2>";
        $shipping_subtotal = 0.0;
        foreach($_POST['shipping'] as $shipping){
            //Sum all the shipping
            $shipping_subtotal += floatval(substr($shipping,1,strlen($shipping)-1));
        }
        echo('<div>The shipping subtotal is $'.number_format($shipping_subtotal,2,'.',',').'</div>');
    }
    else{
        echo "<b>Please Select at least One Option.</b>";
    }
    echo('</div>');
}
?>
HTML


配送选项:

190美元到美国东部的运费
245美元到美国西部的运费
75美元住宅设备费
100美元的尾门服务
将调用


由于
value=“$xxx”
中的美元符号,您无法获得结果。你要么移除它们,要么爆炸分离。如果要去掉美元符号,可以在
echo“$”之后连接$航运然而,我不明白为什么运输应该是一个以上的选项。你最好使用单选按钮,效果很好。我尝试使用$sum+=$shipping,然后在之前使用$sum,但不起作用。我的问题是我从来不知道我必须去掉$sign。谢谢你,佩德罗,谢谢你的回答,这很有效,但我确实想让客户做出几个选择。谢谢你的帮助
<?php
if(isset($_POST['submit'])){
    if(!empty($_POST['shipping'])) {
        echo('<div class="container">');
        //Loop through shipping array to fetch individual shipping so that we can use them
        echo "<h2> Your Shipping Costs are: </h2>";
        /*
        //only if input type="checkbox"
        $shipping_subtotal = 0.0;
        foreach($_POST['shipping'] as $shipping){
            //Sum all the shipping
                        substr();
            $shipping_subtotal += $shipping;
        }
        */
        $shipping_subtotal = (double) $_POST['shipping'];
        echo('<div>The shipping subtotal is $'.number_format($shipping_subtotal,2,'.',',').'</div>');
    }
    else{
        echo "<b>Please Select at least One Option.</b>";
    }
    echo('</div>');
}
?>
<div class="container">
    <form action="tmp.php" method="post">
        <label class="head">Shipping Options:</label><br/><br/>
        <input type="radio" name="shipping" value="190.00"> <label>$190 Freight to Eastern USA</label><br/>
        <input type="radio" name="shipping" value="245.00"><label>$245 Freight to Western USA</label><br/>
        <input type="radio" name="shipping" value="75.00"><label>$75 Residential Devivery</label><br/>
        <input type="radio" name="shipping" value="100.00"><label>$100 Liftgate Service</label><br/>
        <input type="radio" name="shipping" value="0.00"><label>Will Call</label><br/><br/>
        <input type="submit" name="submit" Value="Add to Shopping Cart"/>
    </form>
</div>