PHP Ajax如何正确使用explode

PHP Ajax如何正确使用explode,php,ajax,arrays,string,forms,Php,Ajax,Arrays,String,Forms,我有一个表单,我希望通过ajax和PHP完成计算。我认为我没有正确使用爆炸值。我不知道如何在PHP文件中分离这些内容并返回结果的计算结果。“产品”是通过ajax加载的,所以我们不知道id,但是为了这个示例,我将添加一些 <script type="text/javascript">function test() { var price1682 = 300; var quant1682 = $('#product_quantity_PR

我有一个表单,我希望通过ajax和PHP完成计算。我认为我没有正确使用爆炸值。我不知道如何在PHP文件中分离这些内容并返回结果的计算结果。“产品”是通过ajax加载的,所以我们不知道id,但是为了这个示例,我将添加一些

<script type="text/javascript">function test() {        

        var price1682 = 300;
        var quant1682 = $('#product_quantity_PRI_1682');

        var price2572 = 0;
        var quant2572 = $('#product_quantity_PRI_2572');

        var price2573 = 0;
        var quant2573 = $('#product_quantity_PRI_2573');
                var dataString = 'price1682=' + price1682+'&quant1682=' + quant1682+'&price2572=' + price2572+'&quant2572=' + quant2572+'&price2573=' + price2573+'&quant2573=' + quant2573+'&end=' + 'end' ;
        $.ajax({
            type: 'POST',
             url: 'http://www.divethegap.com/update/wp-content/themes/master/functions/totals.php',
             data: dataString,
             beforeSend: function() {
                 $('#results').html('processing');
                 },
             error: function() {
                 $('#results').html('failure');
                 },
             success: function(alphas) {
                 $('#results').html(alphas);
             }

        });
}</script>
函数测试(){
var价格1682=300;
var quant1682=$(“#产品数量前1682”);
var价格2572=0;
var quant2572=$(“#产品数量前2572”);
var-price2573=0;
var quant2573=$(“#产品数量前2573”);
var dataString='price1682='+price1682+'&quant1682='+quant1682+'&price2572='+price2572+'&Quant25572='+Quant25572+'&price2573='+price2573+'&quant2573='+quant2573+'&quant2573='+quant2573+'&end='+quant2573+'&end=';
$.ajax({
键入:“POST”,
网址:'http://www.divethegap.com/update/wp-content/themes/master/functions/totals.php',
数据:dataString,
beforeSend:function(){
$('#results').html('处理');
},
错误:函数(){
$('#results').html('failure');
},
成功:功能(alphas){
$('#results').html(alphas);
}
});
}
还有PHP

<?php
$str = $_POST['dataString'];
print_r (explode(",",$str));
       ?>

现在结果是
数组([0]=>)
,就是这样

我想要的是将每一个数量乘以每一个价格,然后将所有这些相加,返回一个总数,但目前我甚至无法得到一个有效的数组。显然是哪里出了问题

有什么想法吗

奇妙的

var dataString = 'price[1682]=' + price1682+'&quant[1682]=' + quant1682+'&price[2572]='
+  price2572+'&quant[2572]=' + quant2572+'&price[2573]=' + price2573+'&quant[2573]='
+  quant2573+'&end=' + 'end' ;
现在的结果是:

print_r($_POST['price']);
print_r($_POST['quant']);
UPD:

foreach($price as $id => $p) {
   // Current id
   $id;
   // Current price
   $p;
   // Current quantity
   $_POST['quant'][$id];

   // Miltiply:
   $somevar = $p * $_POST['quant'][$id];
}
别忘了检查所有的
\u POST[price]
/
\u POST[quant]
变量都是整数

最终版本

<?php
$totalprice = 0;
foreach($_POST['price'] as $id => $price) {
   // Current id:
   // $id;
   // Current price:
   // $price;
   // Current quantity:
   // $_POST['quant'][$id];
   // Multiply:
   $somevar = $price * $_POST['quant'][$id];
   $totalprice += $somevar;

}
echo $totalprice;
?>

您正在扩展
而您应该在
&
上扩展。将
分解更改为

explode("&",$str);

它应该会起作用。

打印($\u POST)
会给你什么?你的答案对我有用。只是个问题。我有每个价格和数量的打印数据。现在我想把每个价格乘以相应的数量。请记住,我们没有真实版本的id。你会怎么做?感谢代码工作得很好,但我还需要数组。我试过这个,但也不起作用$totalprice_数组=数组();foreach($\u POST['price']as$id=>$price){$quantity=$\u POST['quant'][$id];$totalproductprice=$price*$quantity;数组推送($totalprice_数组,$totalproductprice);}内爆(',,$totalprice_数组)$totalprice=数组和($totalprice\u数组);