jQuery Ajax POST并返回数据


jQuery Ajax POST and return data

我有一个AJAX提交的表单。

这个表格是有效的,但我觉得功能不正确。

jQuery(document).ready(function(){
    var myForm = $("#ajax_form"), email = $("#email"), emailInfo = $("#emailInfo"), ck1 = $("#ck1"), ck2 = $("#ck2"), ck3 = $("#ck3");
    jQuery('#ajax_form').submit(function(){
        var dados = jQuery( this ).serialize();
        jQuery.ajax({
            type: "POST",
            url: "check.php", // Checking data
            data: dados,
            beforeSend: function(){
                emailInfo.html("<font color='blue'>Checking..</font>");
                if(dados == "email=") // >>> This field, how to check if the field is blank?
                {
                    email.focus();
                    emailInfo.html("<font color='red'>Required.</font>");
                    return false;
                }
            },
            success: function(data){
            if(data == "invalid")
            {
                emailInfo.html("<font color='red'>Invalid.</font>");
            }
            else if(data != "0")
            {
                email.val(data); // This field, how to display the data sent in the email field? not the return of PHP,
                ck1.css("display", "none");
                ck2.css("display", "inline");
            }
            else
            {
                ck1.css("display", "none");
                ck2.css("display", "none");
                ck3.css("display", "inline");
            }
        }
        });
        return false;
    });
});

我认为这有很多错误的代码,例如:

if(dados == "email=") // >>> This field, how to check if the field is blank?

和>>

email.val(data); // This field, how to display the data sent in the email field? not the return of PHP,

我尝试更新,但没有返回任何结果

测试代码

                    //if (email.val() == "")
                //{
                    //email.focus();
                    alert(email.val()); // op1
                    alert(dados); // op2
                    alert($.trim($('email').val())); // op3
                    emailInfo.html("<font color='red'>Required.</font>");
                    return false;
                //}

如果插入电子邮件,则返回的唯一选项是op2电子邮件=teste@teste.com

我认为您的代码试图在提交表单之前通过ajax验证电子邮件。如果是这样,我认为这段代码在几点上是可以的。

提交调用结束时的return false可能无法在firefox上工作。使用e.preventDefault();。看这篇文章。如果你在chrome上尝试这个代码,它可能会失败,因为你在任何地方都没有return true

您的第二个代码块可以。email.val(data);等于$("#email").val(data);。我认为您正在尝试将电子邮件输入值设置为结果。

可以将CCD_ 6改变为CCD_。所以你也不需要爸爸。

您不会在任何地方使用myForm变量。它应该被删除。

如果在服务器端验证电子邮件不是必须考虑在客户端验证。

返回的data值将从PHP文件中返回。有两种方法可以用来验证您的数据:

  1. 在发送表单之前,在前端使用JS进行操作
  2. 在单独的文件中使用PHP代码。

    email.val(data); // This field, how to display the data 
                        sent in the email  field? not the return of PHP
    

    我猜,如果用户发送了一个无效的请求(因此必须再次键入值),您希望确保该值不会被删除。

您可以做的是存储用户在表单提交中输入的值,但在发送AJAX请求之前:var emailVal = email.val();

jQuery(document).ready(function(){
var myForm = $("#ajax_form"), email = $("#email"), emailInfo = $("#emailInfo"), ck1 = $("#ck1"), ck2 = $("#ck2"), ck3 = $("#ck3");
jQuery('#ajax_form').submit(function(){
    var dados = jQuery( this ).serialize();
    var emailVal = email.val(); // Assign the entered input to an emailVal variable
    jQuery.ajax({
        type: "POST",
        url: "check.php", // Checking data
        data: dados,
        beforeSend: function(){
            emailInfo.html("<font color='blue'>Checking..</font>");
            if(dados == "email=") // >>> This field, how to check if the field is blank?
            {
                email.focus();
                emailInfo.html("<font color='red'>Required.</font>");
                return false;
            }
        },
        success: function(data){
        if(data == "invalid")
        {
            emailInfo.html("<font color='red'>Invalid.</font>");
        }
        else if(data != "0")
        {
            email.val(emailVal); // Store the entered value back into the email input
            ck1.css("display", "none");
            ck2.css("display", "inline");
        }
        else
        {
            ck1.css("display", "none");
            ck2.css("display", "none");
            ck3.css("display", "inline");
        }
    }
    });
    return false;
  });
});

另一个注意事项

我还想指出:if(data == "invalid")

我发现PHP可以在data中发回错误消息,以及您要求它返回的任何信息。如果您的PHP代码中有任何错误,这将永远不会发生,因为invalid永远不会是返回的data值中唯一的字符串。为了保护自己,我会做两件事:

  1. 返回HTTP错误,并在AJAX函数的error回调中进行错误处理:https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
  2. 返回一个唯一的单词,并在返回的数据字符串中搜索该单词:

PHP

if(!validEmailCheck($email)){
   echo('invalidRAWR');
}

JS

if(data.indexOf('invalidRAWR') != -1)  // Built in PHP errors will never return 'invalidRAWR'