douchuang1861 2017-11-24 01:50
浏览 73
已采纳

用ajax更新数量

I've tried and succeeded. but there are 2 row, only the first row is updated, not for the second row. if more than one row, the second row and others row will not be updated.

Can anyone help me implement with ajax ?

img

cart.php :

<td>            
    <form method="post" action="cart-update.php">
        <input type="text" name="cart_id" value="<?php echo $row['cart_id']?>">
        <input type="text" name="item_id" value="<?php echo $row['item_id']?>">
        <input type="text" name="id" value="<?php echo $row['cdid']?>">
        <center>
        <button type="submit" class="qtyminus" field="quantity" name="minus" id="value-minus2" onclick="minusqty()">-</button> 
        <input type="text" name="quantity" class="qty" id="value2" value="<?php echo $row['qty']?>">
        <button type="submit" class="qtyplus" field="quantity" name="plus" id="value-plus2" onclick="plusqty()">+</button>  
        </center>
    </form>                     
</td>
//ajax & javascript
<script type="text/javascript">
function minusqty() {//update when press button -
var quantityVal = $("input[name='quantity']").val();
var qtyVal = quantityVal-1; 
var idVal = $("input[name='id']").val();
var itemidVal = $("input[name='item_id']").val();
var cartidVal = $("input[name='cart_id']").val();
$.ajax({
    url: 'cart-update.php',
    method: 'GET',
    data: {qty: qtyVal, item_id: itemidVal, id: idVal, cart_id: cartidVal },
    cache: false,
    dataType: 'html',
    success: function(data) {

    },
});
}
</script>

<script type="text/javascript">
function plusqty() {////update when press button +
var quantityVal2 = $("input[name='quantity']").val();
var qtyVal2 = quantityVal2-(-1); 
var idVal2 = $("input[name='id']").val();
var itemidVal2 = $("input[name='item_id']").val();
var cartidVal2 = $("input[name='cart_id']").val();
$.ajax({
    url: 'cart-update.php',
    method: 'GET',
    data: {qty: qtyVal2, item_id: itemidVal2, id: idVal2, cart_id: cartidVal2 },
    cache: false,
    dataType: 'html',
    success: function(data) {

    },
});
}
</script>

cart-update.php :

<?php
include 'config.php';       

$cart_id = $_GET['cart_id'];
$id      = $_GET['id'];
$item_id = $_GET['item_id'];
$qty     = $_GET['qty']; 

$myqry=mysqli_query($conn,"UPDATE cart_order_detail SET qty='$qty'                                          
                           WHERE cart_id='$cart_id'                                         
                           AND item_id='$item_id'                                           
                           AND id='$id'");

?>

I've tried and succeeded. but there are 2 row, only the first row is updated, not for the second row. if more than one row, the second row and others row will not be updated.

I think its wrong here : var quantityVal = $("input[name='quantity']").val();

  • 写回答

1条回答 默认 最新

  • dsc80135 2017-11-24 02:03
    关注

    On your onclick attributes in html, add event parameter

    id="value-minus2" onclick="minusqty(event)">-</button>
    id="value-plus2" onclick="plusqty(event)">-</button>
    

    On your ajax scripts, add parameter to functions to catch event, then add preventDefault to prevent page refresh

    function minusqty(e) {   // minusqty
        e.preventDefault()
    
    function plusqty(e) {    //plusqty
        e.preventDefault()
    

    Then on minusqty and plusqty ajax success, you do the changing of value of textbox

    success: function(data) {   // minusqty ajax success
        quantityVal = qtyVal;
    },
    
    success: function(data) {   // plusqty ajax success
        quantityVal2 = qtyVal2;
    },
    

    Update:

    Having many quantity fields, you should use

    var quantityVal = $(this).closest("input[name='quantity']").val();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败