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 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错