Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery—将ajax请求合并为拆分请求并替换数据_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript jQuery—将ajax请求合并为拆分请求并替换数据

Javascript jQuery—将ajax请求合并为拆分请求并替换数据,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我是js/jquery新手,所以我认为我做得不错,但我注意到我的代码有一个问题,我认为我需要以某种方式将其合并在一起 如果我输入电子邮件,它会在数据库中更新,但只要我输入firstname或lastname,它就会更新这些内容,然后删除电子邮件 以下是迄今为止的JS: // function to check email field, validate and save to ac for this customer session function checkIt(field) { f

我是js/jquery新手,所以我认为我做得不错,但我注意到我的代码有一个问题,我认为我需要以某种方式将其合并在一起

如果我输入电子邮件,它会在数据库中更新,但只要我输入firstname或lastname,它就会更新这些内容,然后删除电子邮件

以下是迄今为止的JS:

// function to check email field, validate and save to ac for this customer session
function checkIt(field) {
    field = $(field);
    var email = field.val();
    var emailError = "<p>The email address in the <b>E-mail</b> field is invalid.</p>";
    var emailInputId = field.attr('id');
    if ($("." + emailInputId + "_error_message").length > 0) {
        $("." + emailInputId + "_error_message").remove();
    }
    //console.log($(emailInputId+"_error_message"));
    if (validEmail(email)) {
        //alert('valid email');
        $.ceAjax('request', fn_url('ac.email'), {
            method: 'post',
            data: {
                'email': email
            },
            caching: true
        });
        field.removeClass('cm-failed-field');
        field.prev().removeClass('cm-failed-label');
        field.next("span").remove();
    } else {
        field.addClass('cm-failed-field');
        field.prev().addClass('cm-failed-label');
        field.after("<span class='" + emailInputId + "_error_message help-inline' ><p>" + emailError + "</p></span>");
    }
}

// lets check if the email input was already populated, such as browser auto fill etc.. if so use that and save
var field = $('#onestepcheckout .ty-billing-email input')[0];
if ($(field).length > 0) {
    if (field.value) {
        checkIt(field);
    }
}

// check email thats inputted and save to ac session for this customer, or if email changed to update
$('#onestepcheckout .ty-billing-email input').blur(function() {
    checkIt(this);
});

// if first name entered lets grab it and add to the ac session for the customer
var firstname_sel = '#onestepcheckout .ty-billing-first-name input';
var lastname_sel = '#onestepcheckout .ty-billing-last-name input';

$(firstname_sel+','+lastname_sel).blur(function() {
    var firstname = $(firstname_sel).val();
    var lastname = $(lastname_sel).val();
    $.ceAjax('request', fn_url('ac.email'), {
        method: 'post',
        data: {
            'firstname': firstname,
            'lastname': lastname
        },
        caching: true
    });
});

// lets grab the first name and last name if already in input
var firstname_sel_pre = $('#onestepcheckout .ty-billing-first-name input')[0];
var lastname_sel_pre = $('#onestepcheckout .ty-billing-last-name input')[0];
if ($(firstname_sel_pre).length > 0 || $(lastname_sel_pre).length > 0) {
    if (firstname_sel_pre.value || lastname_sel_pre.value) {
        var firstname_pre = $(firstname_sel_pre).val();
        var lastname_pre = $(firstname_sel_pre).val();
        $.ceAjax('request', fn_url('ac.email'), {
            method: 'post',
            data: {
                'firstname': firstname_pre,
                'lastname': lastname_pre
            },
            caching: true
        });
    }
}
更新

我读过关于设置全局变量的书,但我已经尽了最大努力,有点卡住了,我把ajax请求放在了一个新函数中(不确定这是否正确),或者我做得是否正确,但现在有点迷失了如何调用ajax函数

// set global variables
var checkoutEmail = "";
var checkoutFirstName = "";
var checkoutLastName = "";

$(document).ready(function() {

    function fireCheckoutAC(checkoutEmail, checkoutFirstName, checkoutLastName) {
        $.ceAjax('request', fn_url('ac.email'), {
            method: 'post',
            data: {
                'email': checkoutEmail,
                'firstname': checkoutFirstName,
                'lastname': checkoutLastName
            },
            caching: true
        });
    }
    // function to check email field, validate and save to ac for this customer session
    function checkIt(field) {
        field = $(field);
        var email = field.val();
        var emailError = "<p>The email address in the <b>E-mail</b> field is invalid.</p>";
        var emailInputId = field.attr('id');
        if ($("." + emailInputId + "_error_message").length > 0) {
            $("." + emailInputId + "_error_message").remove();
        }
        //console.log($(emailInputId+"_error_message"));
        if (validEmail(email)) {
            //alert('valid email');
            checkoutEmail = email;
            field.removeClass('cm-failed-field');
            field.prev().removeClass('cm-failed-label');
            field.next("span").remove();
        } else {
            field.addClass('cm-failed-field');
            field.prev().addClass('cm-failed-label');
            field.after("<span class='" + emailInputId + "_error_message help-inline' ><p>" + emailError + "</p></span>");
        }
    }

    // lets check if the email input was already populated, such as browser auto fill etc.. if so use that and save
    var field = $('#onestepcheckout .ty-billing-email input')[0];
    if ($(field).length > 0) {
        if (field.value) {
            checkIt(field);
        }
    }

    // check email thats inputted and save to ac session for this customer, or if email changed to update
    $('#onestepcheckout .ty-billing-email input').blur(function() {
        checkIt(this);
    });

    // if first name entered lets grab it and add to the ac session for the customer
    var firstname_sel = '#onestepcheckout .ty-billing-first-name input';
    var lastname_sel = '#onestepcheckout .ty-billing-last-name input';

    $(firstname_sel+','+lastname_sel).blur(function() {
        checkoutFirstName = $(firstname_sel).val();
        checkoutLastName = $(lastname_sel).val();
    });

    // lets grab the first name and last name if already in input
    var firstname_sel_pre = $('#onestepcheckout .ty-billing-first-name input')[0];
    var lastname_sel_pre = $('#onestepcheckout .ty-billing-last-name input')[0];
    if ($(firstname_sel_pre).length > 0 || $(lastname_sel_pre).length > 0) {
        if (firstname_sel_pre.value || lastname_sel_pre.value) {
            checkoutFirstName = $(firstname_sel_pre).val();
            checkoutLastName = $(firstname_sel_pre).val();
        }
    }

});
//设置全局变量
var checkoutEmail=“”;
var checkoutFirstName=“”;
var checkoutLastName=“”;
$(文档).ready(函数(){
函数fireCheckoutAC(checkoutEmail、checkoutFirstName、checkoutLastName){
$.ceAjax('request',fn_url('ac.email'){
方法:“post”,
数据:{
“电子邮件”:checkoutEmail,
“firstname”:checkoutFirstName,
“lastname”:checkoutLastName
},
缓存:true
});
}
//用于检查电子邮件字段、验证并保存到此客户会话的ac的功能
功能检查(字段){
字段=$(字段);
var email=field.val();
var emailError=“电子邮件字段中的电子邮件地址无效。

”; var emailInputId=field.attr('id'); if($(“+emailInputId+”\u错误\u消息”).length>0){ $(“+emailInputId+”\u错误消息”).remove(); } //log($(emailInputId+“\u错误\u消息”); 如果(有效邮件(电子邮件)){ //警报(“有效电子邮件”); checkoutEmail=电子邮件; field.removeClass('cm-failed-field'); field.prev().removeClass('cm-failed-label'); 字段.next(“span”).remove(); }否则{ field.addClass('cm-failed-field'); field.prev().addClass('cm-failed-label'); 字段。在(“”+emailError+“

”)之后; } } //让我们检查电子邮件输入是否已填充,如浏览器自动填充等。如果已填充,请使用该选项并保存 var字段=$('#onestepcheckout.ty计费电子邮件输入')[0]; 如果($(字段).length>0){ if(字段值){ 选中它(字段); } } //检查输入的电子邮件并保存到此客户的ac会话,或者电子邮件是否更改为更新 $('#onestepcheckout.ty计费电子邮件输入').blur(函数(){ 检查(这个); }); //如果输入了名字,让我们抓取它并添加到客户的ac会话中 var firstname_sel='#onestepcheckout.ty账单名字输入'; var lastname_sel='#onestepcheckout.ty计费姓氏输入'; $(firstname_sel+,'+lastname_sel).blur(函数(){ checkoutFirstName=$(firstname_sel).val(); checkoutLastName=$(lastname_sel).val(); }); //如果输入中已经有名字和姓氏,让我们抓取 var firstname_sel_pre=$(“#onestepcheckout.ty账单名字输入”)[0]; var lastname_sel_pre=$('#onestepcheckout.ty billing last name input')[0]; 如果($(firstname_sel_pre).length>0 | |$(lastname_sel_pre).length>0){ if(firstname_sel_pre.value | | lastname_sel_pre.value){ checkoutFirstName=$(firstname_sel_pre).val(); checkoutLastName=$(firstname_sel_pre).val(); } } });
我找到了解决方案,它是:

/* grab completed email when enetred into checkout and add to abandoned cart for that session */
function validEmail(v) {
    var r = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return (v.match(r) == null) ? false : true;
}

// set global variables
var checkoutEmail = "";
var checkoutFirstName = "";
var checkoutLastName = "";

$(document).ready(function() {

    function fireCheckoutAC() {
        $.ceAjax('request', fn_url('ac.email'), {
            method: 'post',
            data: {
                'email': checkoutEmail,
                'firstname': checkoutFirstName,
                'lastname': checkoutLastName
            },
            caching: true
        });
    }
    // function to check email field, validate and save to ac for this customer session
    function checkIt(field) {
        field = $(field);
        var email = field.val();
        var emailError = "<p>The email address in the <b>E-mail</b> field is invalid.</p>";
        var emailInputId = field.attr('id');
        if ($("." + emailInputId + "_error_message").length > 0) {
            $("." + emailInputId + "_error_message").remove();
        }
        //console.log($(emailInputId+"_error_message"));
        if (validEmail(email)) {
            //alert('valid email');
            checkoutEmail = email;
            fireCheckoutAC();
            field.removeClass('cm-failed-field');
            field.prev().removeClass('cm-failed-label');
            field.next("span").remove();
        } else {
            field.addClass('cm-failed-field');
            field.prev().addClass('cm-failed-label');
            field.after("<span class='" + emailInputId + "_error_message help-inline' ><p>" + emailError + "</p></span>");
        }
    }

    // lets check if the email input was already populated, such as browser auto fill etc.. if so use that and save
    var field = $('#onestepcheckout .ty-billing-email input')[0];
    if ($(field).length > 0) {
        if (field.value) {
            checkIt(field);
        }
    }

    // check email thats inputted and save to ac session for this customer, or if email changed to update
    $('#onestepcheckout .ty-billing-email input').blur(function() {
        checkIt(this);
    });

    // if first name entered lets grab it and add to the ac session for the customer
    var firstname_sel = '#onestepcheckout .ty-billing-first-name input';
    var lastname_sel = '#onestepcheckout .ty-billing-last-name input';

    $(firstname_sel+','+lastname_sel).blur(function() {
        checkoutFirstName = $(firstname_sel).val();
        checkoutLastName = $(lastname_sel).val();
        fireCheckoutAC();
    });

    // lets grab the first name and last name if already in input
    var firstname_sel_pre = $('#onestepcheckout .ty-billing-first-name input')[0];
    var lastname_sel_pre = $('#onestepcheckout .ty-billing-last-name input')[0];
    if ($(firstname_sel_pre).length > 0 || $(lastname_sel_pre).length > 0) {
        if (firstname_sel_pre.value || lastname_sel_pre.value) {
            checkoutFirstName = $(firstname_sel_pre).val();
            checkoutLastName = $(firstname_sel_pre).val();
            fireCheckoutAC();
        }
    }

});
/*在登录到收银台时抓取已完成的电子邮件,并添加到该会话的废弃购物车中*/
功能验证码(v){
在新一个regEXR r r=新的regEXR r r=新的RegExp(((((“[\w-\w-[[[w-[[w-[[[w-[s-))的r r=新的regEXR r r r=新的RegExp((((“[[[[[[[w-\w-\\s-\s[s-\s][[[[[[s-\s[s][[[[s][[[[[[[s][[[s][[3]]]]]]的+)))))))))的)号号(((((((()))))(([[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[|[0-9]{1,2}\)((25[0-5]{2[0-4][0-9]{2}{0-9]{1,2}\){2}(25[0-5]{2[0-4][0-9]{1[0-9]{2}{1,2}}})/);
返回值(v.match(r)==null)?false:true;
}
//设置全局变量
var checkoutEmail=“”;
var checkoutFirstName=“”;
var checkoutLastName=“”;
$(文档).ready(函数(){
函数fireCheckoutAC(){
$.ceAjax('request',fn_url('ac.email'){
方法:“post”,
数据:{
“电子邮件”:checkoutEmail,
“firstname”:checkoutFirstName,
“lastname”:checkoutLastName
},
缓存:true
});
}
//用于检查电子邮件字段、验证并保存到此客户会话的ac的功能
功能检查(字段){
字段=$(字段);
var email=field.val();
var emailError=“电子邮件字段中的电子邮件地址无效。

”; var emailInputId=field.attr('id'); if($(“+emailInputId+”\u错误\u消息”).length>0){ $(“+emailInputId+”\u错误消息”).remove(); } //log($(emailInputId+“\u错误\u消息”); 如果(有效邮件(电子邮件)){ //警报(“有效电子邮件”); checkoutEmail=电子邮件; fireCheckoutAC(); field.removeClass('cm-failed-field'); field.prev().removeClass('cm-failed-label'); 字段.next(“span”).remove(); }否则{ field.addClass('cm-failed-field'); field.prev().addClass('cm-failed-label'); 字段。在(“”+emailError+“

”)之后; } } //让我们检查电子邮件输入是否已填充,如浏览器自动填充等。如果已填充,请使用该选项并保存 var字段=$('#onestepcheckout.ty计费电子邮件输入')[0]; 如果($(字段).length>0){ if(字段值){ 选中它(字段); } } //检查输入的电子邮件并保存到此客户的ac会话,或者电子邮件是否更改为更新 $('#onestepcheckout.ty计费电子邮件输入').blur(函数(){ 检查(这个); });
/* grab completed email when enetred into checkout and add to abandoned cart for that session */
function validEmail(v) {
    var r = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return (v.match(r) == null) ? false : true;
}

// set global variables
var checkoutEmail = "";
var checkoutFirstName = "";
var checkoutLastName = "";

$(document).ready(function() {

    function fireCheckoutAC() {
        $.ceAjax('request', fn_url('ac.email'), {
            method: 'post',
            data: {
                'email': checkoutEmail,
                'firstname': checkoutFirstName,
                'lastname': checkoutLastName
            },
            caching: true
        });
    }
    // function to check email field, validate and save to ac for this customer session
    function checkIt(field) {
        field = $(field);
        var email = field.val();
        var emailError = "<p>The email address in the <b>E-mail</b> field is invalid.</p>";
        var emailInputId = field.attr('id');
        if ($("." + emailInputId + "_error_message").length > 0) {
            $("." + emailInputId + "_error_message").remove();
        }
        //console.log($(emailInputId+"_error_message"));
        if (validEmail(email)) {
            //alert('valid email');
            checkoutEmail = email;
            fireCheckoutAC();
            field.removeClass('cm-failed-field');
            field.prev().removeClass('cm-failed-label');
            field.next("span").remove();
        } else {
            field.addClass('cm-failed-field');
            field.prev().addClass('cm-failed-label');
            field.after("<span class='" + emailInputId + "_error_message help-inline' ><p>" + emailError + "</p></span>");
        }
    }

    // lets check if the email input was already populated, such as browser auto fill etc.. if so use that and save
    var field = $('#onestepcheckout .ty-billing-email input')[0];
    if ($(field).length > 0) {
        if (field.value) {
            checkIt(field);
        }
    }

    // check email thats inputted and save to ac session for this customer, or if email changed to update
    $('#onestepcheckout .ty-billing-email input').blur(function() {
        checkIt(this);
    });

    // if first name entered lets grab it and add to the ac session for the customer
    var firstname_sel = '#onestepcheckout .ty-billing-first-name input';
    var lastname_sel = '#onestepcheckout .ty-billing-last-name input';

    $(firstname_sel+','+lastname_sel).blur(function() {
        checkoutFirstName = $(firstname_sel).val();
        checkoutLastName = $(lastname_sel).val();
        fireCheckoutAC();
    });

    // lets grab the first name and last name if already in input
    var firstname_sel_pre = $('#onestepcheckout .ty-billing-first-name input')[0];
    var lastname_sel_pre = $('#onestepcheckout .ty-billing-last-name input')[0];
    if ($(firstname_sel_pre).length > 0 || $(lastname_sel_pre).length > 0) {
        if (firstname_sel_pre.value || lastname_sel_pre.value) {
            checkoutFirstName = $(firstname_sel_pre).val();
            checkoutLastName = $(firstname_sel_pre).val();
            fireCheckoutAC();
        }
    }

});