2

我正在使用 Ajax、Javascript 和 PHP/MySQL 构建购物车。在主购物车页面上,用户可以单击图像链接以从他们的每个项目选择中添加 1 或删除 1。Ajax 使用 PHP 脚本向/从数据库中该项目的金额添加 1 或删除 1,并且 javascript 对购物车页面执行相同的操作。我遇到的问题是,当我在这些图像链接中的任何一个上单击太快时,我的数据库中存在的值与屏幕上存在的值不再匹配。我认为这将是一个相当容易解决的问题,但我似乎无法正确解决。我尝试在 PHP 脚本中使用 usleep(),在 javascript 中使用 setTimeout(),并且我尝试设置一个 cookie(更新过程开始时值 = 1,完成时值 = 0)并且只允许该过程如果值 != 1 则运行,但这都没有解决我的问题。代码如下。

购物车.php

<a onClick='itemAddOne($item[id])' class='item_add'><img src='images/cart-add.jpg' alt='' onClick=this.src='images/cart-add_click.jpg' onMouseOut=this.src='images/cart-add.jpg'></img></a>

购物车.js

var xmlhttp;
var sub_v;
var sub_v2;
var add_tot;
var element
var item_add;
var resp;
var prov;
var taxrate;
var finish;

function itemAddOne(itemID) {
    if (window.finish != 1) {
        finish = 1;
        document.cookie = "finish=1; path=/";
        xmlhttp.open("POST","add.php",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    resp = xmlhttp.responseText;
                    add_tot = resp.substr(3);
                    prov = resp.substr(0,2);
                    if (prov == 'BC') {
                        taxrate = .12;
                    } else if (prov == 'AB') {
                        taxrate = .05;
                    } else if (prov == 'SK') {
                        taxrate = .05;
                    } else if (prov == 'MB') {
                        taxrate = .05;
                    } else if (prov == 'ON') {
                        taxrate = .13;
                    } else if (prov == 'QC') {
                        taxrate = .05;
                    } else if (prov == 'NL') {
                        taxrate = .13;
                    } else if (prov == 'NS') {
                        taxrate = .15;
                    } else if (prov == 'NB') {
                        taxrate = .13;
                    } else if (prov == 'PE') {
                        taxrate = .05;
                    } else if (prov == 'NT') {
                        taxrate = .05;
                    } else if (prov == 'NU') {
                        taxrate = .05;
                    } else if (prov == 'YT') {
                        taxrate = .05;
                    }
                    element = "item_quantity" + itemID;
                    item_add = document.getElementById(element);
                    tax_v = document.getElementById('tax');
                    tot_v = document.getElementById('tot');
                    sub_v = document.getElementById('subt');
                    item_add.value = Number(item_add.value) + 1;
                    sub_v.value = (Number(sub_v.value) + Number(add_tot));
                    tax_v.value = Number(sub_v.value) * Number(taxrate);
                    tot_v.value = Number(tax_v.value) + Number(sub_v.value);
                    tax_v.value = Number(tax_v.value).toFixed(2);
                    sub_v.value = Number(sub_v.value).toFixed(2);
                    tot_v.value = Number(tot_v.value).toFixed(2);
                }
            }
        };
        xmlhttp.send("itemid=" + itemID + "&amount=1");
        finish = 0;
        document.cookie = "finish=0; path=/";
    }
}

添加.php

    <?
if ($_REQUEST['itemid'] && $_REQUEST['amount'] && $_COOKIE['finish'] != 1) {

    require 'requires/connect.php';
    require 'requires/function.php';


    $_REQUEST['itemid'] = protect($_REQUEST['itemid'],'2');
    $_REQUEST['amount'] = protect($_REQUEST['amount'],'2');

    $sql = "SELECT id, ws_price FROM items WHERE id = '$_REQUEST[itemid]'";
    $sql2 = query($sql);
    $sql3 = mysql_numrows($sql2);
    $add_item = mysql_fetch_array($sql2);
    $add_item['ws_price'] = $add_item['ws_price'] * $_REQUEST['amount'];

    if ($sql3 > 0) {

            if (strlen($_COOKIE['ua_email']) > 0 && strlen($_COOKIE['ua_pass']) > 0) {

            $_COOKIE['ua_email'] = protect($_COOKIE['ua_email'],'1');
            $_COOKIE['ua_pass'] = protect($_COOKIE['ua_pass'],'1');

            $sql = "SELECT id, province FROM user_account WHERE email = '$_COOKIE[ua_email]' AND pass = '$_COOKIE[ua_pass]'";
            $sql2 = query($sql);
            $sql3 = mysql_numrows($sql2);

            if ($sql3 > 0) {

                $user = mysql_fetch_array($sql2);

                $sql = "SELECT orderid FROM orders WHERE userid = '$user[id]'";
                $sql2 = query($sql);
                $sql3 = mysql_numrows($sql2);

                if ($sql3 > 0) {
                    $sql4 = mysql_fetch_array($sql2);
                    $order_id = $sql4['orderid'];
                } else {
                    $sql = "SELECT orderid FROM orders ORDER BY orderid DESC";
                    $sql2 = query($sql);
                    $sql3 = mysql_fetch_array($sql2);
                    $order_id = $sql3['orderid'] + 1;
                }

                $sql = "SELECT id, amount FROM orders WHERE userid = '$user[id]' AND orderid = '$order_id' AND itemid = '$_REQUEST[itemid]'";
                $sql2 = query($sql);
                $sql3 = mysql_numrows($sql2);

                if ($sql3 > 0) {
                    $sql4 = mysql_fetch_array($sql2);

                    if (($sql4['amount'] + $_REQUEST['amount']) > 0) {
                        $sql = "UPDATE orders SET amount = amount + $_REQUEST[amount] WHERE id = '$sql4[id]'";
                        $sql2 = query($sql);
                        echo "$user[province]-$add_item[ws_price]";
                    }

                } else {

                $sql = "INSERT INTO orders (orderid, itemid, userid, amount) VALUES ('$order_id', '$_REQUEST[itemid]', '$user[id]', '$_REQUEST[amount]')";
                $sql2 = query($sql);

                }

            }
        }
    }
}
?>

我已经搜索并且无法想出解决此问题的简单方法,任何帮助将不胜感激!

4

1 回答 1

0

解决这个问题就像使请求同步而不是异步一样简单。由于请求现在是同步的,它会强制每个请求在允许另一个实例开始之前完成,因此脚本现在的运行速度不可能超过它们在屏幕上更新的速度。干杯!

于 2012-11-19T23:27:22.110 回答