Ajax更新数量,其中ID和数量 [英] ajax update qty where id and qty

查看:37
本文介绍了Ajax更新数量,其中ID和数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新具有多个项目的购物车中的数量

i am trying to update quantity in a cart where there are multiple items,

例如-

var get id of order item
var get quantity
post both to updateqty.php

按顺序显示多行,有多个文本框的数量为qty和多个隐藏的输入的部分为id,因此它只能在该页面上找到的第一个id上工作,我需要它能够更新订单中的其他项目

with multiple lines on order there are multiple textboxes with the id of qty and multiple hidden inputs with the id of part so it will only work on the first id that it finds on that page, i need it to be able to update other items in the order,

例如

get class of the textbox, then find out which qty is being updated, then update the quantity of the item.

如果有人对如何做到这一点有所了解,将不胜感激.

if anyone has an idea how this can be done it would be appreciated.

<script>


function updateqty() {
    var ud_first = document.getElementById('part').value;
    var ud_last = document.getElementById('qty').value;

    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("cart").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "php/updateqty.php?order=<?php echo $order; ?>&id=" + ud_first + "&qty="+ud_last, true);
    xmlhttp.send();
}
</script>
<?php 


$sql="SELECT * FROM `orders` WHERE  `invoice` = '".$order."' ORDER BY id DESC";

$result = mysql_query($sql);
echo"
     <table id='POITable' width='100%' border='1'>
        <tr>
            <td>SKU</td>
            <td>QTY</td>
            <td width='45%'>item</td>
            <td>Unit Price</td>
            <td>Line Price</td>
            <td>Delete</td>
        </tr>";
while($row = mysql_fetch_array($result))
  {
echo"<tr><td>" . $row['part'] . "</td><td><form name='test'>
   <input type='hidden' value='" . $row[id] . "' id='part'>   <input type='text' id='qty' value='" . $row['qty'] . "' onblur='updateqty(this.id)'></form></td><td>" . $row['description'] . "</td><td>" . $row['price'] . "</td><td>" . $row['lineprice'] . "</td><td> <input type='image' src='images/btn_delete.png' value='" . $row[id] . "' onclick='deletesku(this.value)' height='30'/></td>
";

 }
?>  

插入php是

$id=$_GET[id];

$sql2="SELECT * FROM  `orders` WHERE  `id` = '".$id."'";

$result2 = mysql_query($sql2);

$row2 = mysql_fetch_array($result2);

$order=$_GET[order];
$qty=$_GET[qty];

$sql="SELECT * FROM  `stock` WHERE  `part` = '".$part."'";

$result = mysql_query($sql);

$row1 = mysql_fetch_array($result);

$lineprice=$qty * $row2[price];

$sqlins1 = "UPDATE `orders` SET qty='$qty', lineprice='$lineprice' WHERE id = '".$id."'";

它是html中的ID,不是MYSQL表的倍数

it is ids in the html that there are multiples of NOT THE MYSQL TABLE

所以我需要它来拾取购物车中产品的ID并通过Ajax更新该ID的数量

so i need it to pick up the id of a product in the cart and update the quantity of that id by ajax

脚本的重要部分是

    function updateqty() {
        var ud_first = document.getElementById('part').value;
        var ud_last = document.getElementById('qty').value;

        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("cart").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "php/updateqty.php?order=<?php echo $order; ?>&id=" + ud_first + "&qty="+ud_last, true);
        xmlhttp.send();
    }
    </script>
    <?php 


    $sql="SELECT * FROM `orders` WHERE  `invoice` = '".$order."' ORDER BY id DESC";

    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result))
      {
    echo"<tr><td>" . $row['part'] . "</td>

<td>

    <form name='test'>
           <input type='hidden' value='" . $row[id] . "' id='part'>
       <input type='text' id='qty' value='" . $row['qty'] . "' onblur='updateqty(this.id)'></form>

    </td><td>" . $row['description'] . "</td><td>" . $row['price'] . "</td><td>" . $row['lineprice'] . "</td><td> <input type='image' src='images/btn_delete.png' value='" . $row[id] . "' onclick='deletesku(this.value)' height='30'/></td>
        ";

         }

预先感谢

推荐答案

    function updateqty() {  

    var str="$(this).attr('name')"; 
    var orderCode=str.slice(9,-1);
    var quantity = $(this).value;  

          if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {

    xmlhttp.open("GET","updateqty.php?order=<?php echo $order; ?>&id=" + orderCode + "&qty=" + quantity,true);
    xmlhttp.send();
    }

    </script>


    <div id="cart">
    <?php
    $sql="SELECT * FROM `orders` WHERE  `invoice` = '".$order."' ORDER BY id DESC";

    $result = mysql_query($sql);
    echo"
       <table id='POITable' width='100%' border='1'>
            <tr>
                <td>SKU</td>
                <td>QTY</td>
                <td width='45%'>item</td>
                <td>Unit Price</td>
                <td>Line Price</td>
                <td>Delete</td>
            </tr>";
    while($row = mysql_fetch_array($result))
      {
    echo"<tr><td>" . $row['part'] . "</td><td>
    <input type='text' name='quantity[779]' size='3' value='" . $row['qty'] . "' tabindex='1' onblur='updateqty(this.value)' />

</td><td>" . $row['description'] . "</td><td>" . $row['price'] . "</td><td>" . $row['lineprice'] . "</td><td><img src='images/btn_delete.png' onclick='deleteRow(this)' height='30'/></td>
";
 }

这篇关于Ajax更新数量,其中ID和数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆