更改数量后,购物车不会更新

时间:2017-12-30 17:22:44

标签: php ajax post cart

我正在使用PHP和AJAX制作购物车系统。一切都很好,除了更新部分。当用户点击数字表单之外时,小计将自动更新。我使用AJAX,但不起作用。我用搜索测试了这一切,一切都很好。

AJAX功能:

function initXML() { //Adaptation for old browsers
    var _xmlhttp;
    if (window.XMLHttpRequest) {
        _xmlhttp = new XMLHttpRequest();
    } else {
        _xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    return _xmlhttp;
}
function ajaxFormValidate(_config) {
    /*Structure
        type: 'GET' or 'POST',
        url: 'request URL' Default: location.href,
        method: true or false (optional). False for non-async, true for async,
        sendItem: file or data to be sent,
        success: a callback function when the request is complete
        error: a fallback function when the request is failed
    */
    if (!_config.type) {
        _config.type = 'POST'; //Automatically set type to POST if no type property is declared
    }
    if (!_config.url) {
        _config.url = location.href; //Automatically set url to self if no url property is declared
    }
    if (!_config.method) {
        _config.method = true; //Automatically set method to true if no method property is declared
    }
    var _xmlHttp = initXML(); //Declare request variable
    _xmlHttp.onreadystatechange = function(){
        if (_xmlHttp.readyState === 4 && _xmlHttp.status === 200) {
            if (_config.success) {
                _config.success(_xmlHttp.responseText);
            }
        }
        else {
            if (_config.error) {
                _config.error(_xmlHttp.responseText);
            }
        }
    }; //Check readyState and status to handle the request properly
    //Handle the items sent
    var _Itemstring = [], _sendItem = _config.sendItem;
    if (typeof _sendItem === "string") {
        var _arrTmp = String.prototype.split.call(_sendItem, '&');
        for (var i = 0; i < _arrTmp.length; i ++) {
            var _tmpData = _arrTmp[i].split('=');
            _Itemstring.push(encodeURIComponent(_tmpData[0]) + "=" + encodeURIComponent(_tmpData[1]));
        }
    }
    else if (typeof _sendItem === "object" && !(_sendItem instanceof String || (FormData && _sendItem instanceof FormData))) {
        for (var k in _sendItem) {
            var _tmpData = _sendItem[k];
            if (Object.prototype.toString.call(_tmpData) === "[object Array]") {
                for (var j = 0; j < _tmpData.length; j ++) {
                    _Itemstring.push(encodeURIComponent(k) + '[]=' + encodeURIComponent(_tmpData[j]));
                }
            }
            else {
                _Itemstring.push(encodeURIComponent(k) + '=' + encodeURIComponent(_tmpData));
            }
        }
    }
    _Itemstring = _Itemstring.join('&');
    if (_config.type === 'GET') {
        _xmlHttp.open('GET', _config.url + "?" + _Itemstring, _config.method);
        _xmlHttp.send();
    }
    else if (_config.type === 'POST') {
        _xmlHttp.open('POST', _config.url, _config.method);
        _xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        _xmlHttp.send(_Itemstring);
    }
}

AJAX在处理输入变化的JS文件中调用

// JavaScript Document
window.addEventListener('load', function(){
    var _ranum = document.getElementsByClassName('ranum');
    for (var i = 0; i < _ranum.length; i ++) {
        _ranum[i].addEventListener('blur', function(){ //Check click outside
            var _this = this;
            ajaxFormValidate({
                type:'POST', 
                sendItem: {
                    u: _this.value, //Send the value after the change
                    id: _this.id, //Send the product id
                },
                success: function(response){
                    console.log('SUCCESS');
                }
            });
        }, false);
    }
}, false);

处理PHP文件中的更改

var_dump($_POST['u']);    
if (isset($_POST['id'], $_POST['u'])) {
        if (!empty($_POST['id']) && !empty($_POST['u'])) {
            $id = mysqli_real_escape_string($conn, $_POST['id']);
            $u = mysqli_real_escape_string($conn, $_POST['u']);
            if (isset($_SESSION['cart'][$id])) {
                $_SESSION['cart'][$id] = $u;
            }
        }
    } 

我看到它在控制台中注销了“SUCCESS”,但是,当我使用var_dump($_POST['u'])时,它无效。此外,它仅在我重新加载页面时更新小计。 我做错了什么?我很确定我的AJAX功能是正确的,并且JS注销了'SUCCESS',那么问题是什么?非常感谢

0 个答案:

没有答案