Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在一个页面上生成多个产品的总价_Php_Jquery - Fatal编程技术网

Php 在一个页面上生成多个产品的总价

Php 在一个页面上生成多个产品的总价,php,jquery,Php,Jquery,我正在尝试生成一页商店的总价格 我在同一页上有不同的产品,在foreach中如下所示: <input type="hidden" name="price_<?=$product_id?>" value="<?=$product_price?>" /> <input type="text" name="<?=$product_id?>" value="" onchange="calculateTotal();" /> 注意,使用HTML

我正在尝试生成一页商店的总价格

我在同一页上有不同的产品,在foreach中如下所示:

<input type="hidden" name="price_<?=$product_id?>" value="<?=$product_price?>" />
<input type="text" name="<?=$product_id?>" value="" onchange="calculateTotal();" />

注意,使用HTML存储价格变量通常是不礼貌的,为什么不保持所有价格的运行总计?差不多

$running_total = $running_total+$product_price;
这将输出经过循环的所有产品的总数。如果我误解了你的问题,我很抱歉


将价格存储为要通过POST或GET传递的HTML实体时,最终用户可以对该变量进行操作。

按照自己的意愿进行操作。只要您在结帐时(再次?在服务器上执行计算

为什么不使用新的
数据
属性并跳过隐藏的价格字段?大概是这样的:

<input class='quantity' type="number" data-price="1.5"  name="prod1" value="" />
<div class='subtotal'>0.00</div>

<input class='quantity' type="number" data-price="7"  name="prod2" value="" />
<div class='subtotal'>0.00</div>

<div id='total'>0.00</div>

我会在服务器上做所有的计算(业务逻辑)。你有没有想过你的问题?你接受答案吗?这就是我一直在寻找的。但是以表格的形式。
$('.quantity').on('change', function(){
    var sub = $(this).val() * $(this).data('price');
    $(this).next('div.subtotal').html(sub).data('sub',sub);
    var tot=0;
    $('.subtotal').each(function(){
            tot+= $(this).data('sub');
        });
    $('div#total').html(tot);
});