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 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题