php ajax多个数据请求

时间:2018-03-09 20:08:24

标签: php jquery ajax

我在使用ajax获取多个数据时遇到问题。

我有购物车,如果我添加(+)更多数量然后崩溃,一个数据参数它可以正常工作,但如果我尝试多次,它会给出奇怪的大数字。

如果我不使用增值税+总费用,那么它可行,但如果我使用增值税+总计我得到多个点数..

示例:251.52254.5141.92 //不含增值税且总成本价格为251,52(251.52 254.51 41.92) 例2:262264.9943.6666666667 //增加了一个数量......

<div class="table-content table-responsive mb-50">
  <table class="text-center">
      <thead>
          <tr>
              <th class="product-thumbnail">Product</th>
              <th class="product-price">Price</th>
              <th class="product-quantity">QTY</th>
              <th class="product-subtotal">subtotal</th>
              <th class="product-remove">RMV</th>
          </tr>
      </thead>
      <tbody>
        <tr id="row<?php echo $i;?>">
          <td class="product-thumbnail">
              <div class="pro-thumbnail-img">
                  <img width="75" src="<?php echo "$baseurl/productimages/$ppp[img]";?>" alt="<?php echo $ppp['name']; ?>">
              </div>
              <div class="pro-thumbnail-info text-left">
                  <h6 class="product-title-2">
                      <a href="<?php echo $urrl; ?>"><?php echo $ppp['name'];?></a>
                  </h6>
                  <p>Brand: <?php echo $subcat['name']; ?></p>
              </div>
          </td>
          <td class="product-price"><?php echo number_format(round($data['rraate'], 2), 2) . " " . $curr['currency']; ?></td>
          <td class="product-quantity">
            <button id="btnminus<?php echo $i;?>" onClick="var result = document.getElementById('qty<?php echo $i;?>'); var qty<?php echo $i;?> = result.value; if( !isNaN( qty<?php echo $i;?> ) &amp;&amp; qty<?php echo $i;?> &gt; <?php echo $ppp['minbuy']; ?> ) result.value--;return false;" class="reduced items-count" typy="button"><i class="zmdi zmdi-minus">&nbsp;</i></button>
              <input type="text" id="qty<?php echo $i;?>" class="input-text qty" title="Qty" value="<?php echo $data['qty']; ?>" min="<?php echo $ppp['minbuy']; ?>" maxlength="12" id="qty<?php echo $i;?>" name="qty" style="display:inline-block; width: 70px;">
            <button id="btnplus<?php echo $i;?>" onClick="var result = document.getElementById('qty<?php echo $i;?>'); var qty<?php echo $i;?> = result.value; if( !isNaN( qty<?php echo $i;?> )) result.value++;return false;" class="increase items-count" type="button"><i class="zmdi zmdi-plus">&nbsp;</i></button>
          </td>
          <input type="hidden" id="id" name="id" value="<?php echo $iidd; ?>">
          <input type="hidden" id="unique" name="unique" value="<?php echo $unique; ?>">
          <td class="product-subtotal" id="chk<?php echo $i;?>"><?php echo $ttl . " " . $curr['currency']; ?></td> <!-- subtotal id -->
          <td class="product-remove">
              <a id="btnrmv<?php echo $i;?>" href="#" title="Eemalda toode"><i class="zmdi zmdi-close"></i></a>
          </td>
      </tr>
    </tbody>
  </table>
</div>
<div class="row">
  <div class="col-md-6 pull-right">
      <div class="payment-details box-shadow p-30 mb-50">
          <h6 class="widget-title border-left mb-20">Payment</h6>
          <table>

              <tr>
                  <td class="td-title-1">Delivery price</td>
                  <td class="td-title-2"><?php echo $tarne . " " . $curr['currency']; ?></td>
              </tr>
              <tr>
                  <td class="td-title-1">VAT(20%)</td>
                  <td class="td-title-2" id="km<?php echo $i;?>"><?php echo  $km . " " . $curr['currency']; ?></td> <!-- VAT id -->
              </tr>
              <tr>
                  <td class="order-total">Total</td>
                  <td class="order-total-price" id="sum<?php echo $i;?>"><?php echo $sum . " " . $curr['currency']; ?></td> <!-- SUM id -->
              </tr>
          </table>
      </div>
  </div>
</div>

的Ajax:

$(document).ready(function() {                           
  $("#btnplus<?php echo $i;?>").click(function() {   
    $.post("<?php echo $baseurl;?>/api-cart-update.php",{ 
      unique: "<?php echo $unique;?>",
      cccid: "<?php echo $data['id'];?>",
      act: "plus"
    },

      function(data) {
        $('#chk<?php echo $i;?>').html(data);
        $('#sum<?php echo $i;?>').html(data);
        $('#km<?php echo $i;?>').html(data);
      });
  });

  $("#btnminus<?php echo $i;?>").click(function() {
    $.post("<?php echo $baseurl;?>/api-cart-update.php",{ 
    unique: "<?php echo $unique;?>",
    cccid: "<?php echo $data['id'];?>",
    act: "minus"
  },

    function(data) {
      $('#chk<?php echo $i;?>').html(data);
      $('#sum<?php echo $i;?>').html(data);
      $('#km<?php echo $i;?>').html(data);
    });
  });
});

API-车-update.php:

require_once('function.php');

if(isset($_POST['unique'])) {

$un = $_POST["unique"];
$act = $_POST["act"];

try{
    $database = new Connection();
    $db = $database->openConnection();
    $sql = "SELECT code FROM carrrt WHERE id = :cccid";
    $qry1 = $db->prepare($sql);
    $qry1 -> bindParam(':cccid', $_POST["cccid"], PDO::PARAM_INT);
    $qry1 -> execute();
    $security = $qry1->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    echo "There is some problem in connection: " . $e->getMessage();
}

if($security['code']==$un){

try{
    $database = new Connection();
    $db = $database->openConnection();
    $sql= "SELECT qty, rraate FROM carrrt WHERE id = :cccid";
    $qry = $db->prepare($sql);
    $qry -> bindParam(':cccid', $_POST["cccid"], PDO::PARAM_INT);
    $qry -> execute();
    $qnow = $qry->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    echo "There is some problem in connection: " . $e->getMessage();
}

$sum=0;
$tarne=2.99;

if($act=="plus"){
    $will = $qnow['qty']+1; 


}else{
    $will = $qnow['qty']-1;

}

if($will<=1){
$will=1;    
}
try{
    $database = new Connection();
    $db = $database->openConnection();
    $sql = "UPDATE carrrt SET qty = :qty WHERE id = :cccid";
    $stmt = $db->prepare($sql);
    $stmt->bindParam(':cccid', $_POST['cccid'], PDO::PARAM_INT); 
    $stmt->bindParam(':qty', $will, PDO::PARAM_INT); 
    $stmt -> execute();
} catch (PDOException $e) {
    echo "There is some problem in connection: " . $e->getMessage();
}

$subttl = $qnow['rraate']*$will;

$sum += $subttl + $tarne;
$vat = 20;
$vatDivisor = 1 + ($vat / 100);
$priceBeforeVat = $subttl / $vatDivisor;
$km = $subttl - $priceBeforeVat;



if($stmt){
    echo "$subttl";
    echo "$sum";
    echo "$km";

}

}
}

1 个答案:

答案 0 :(得分:0)

试试这个:

$.ajax({
    url: 'YOUR URL',
    type: 'post',
    dataType: 'json',
    data: {
        'POST1': 'variable',
        'POST2': 'variable'
    },
    success: function(data) {
        alert(data.array1);
    }
});

在你的PHP文件上

if (isset($_POST['POST1'])) {
  //'DO SOME THING';
  json_encode(array(array1=>'bla bla',array2=>'ble ble'));
}