Ajax Javascript PHP - 点击太快破坏数字

时间:2012-11-19 00:22:45

标签: php javascript ajax

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

cart.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>

cart.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=/";
    }
}

add.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);

                }

            }
        }
    }
}
?>

我已经搜索过,无法想出一个简单的方法来解决这个问题,任何帮助都将不胜感激!

1 个答案:

答案 0 :(得分:0)

修复此问题就像使请求同步而不是异步一样简单。现在请求是同步的,它会在允许另一个实例开始之前强制完成每个请求,因此脚本现在可能无法比在屏幕上更新更快地运行。干杯!