2016-03-28 122 views
1

我使用AJAX提交表单。jQuery Ajax POST并返回数据

此表格正在工作,但我有一个印象是功能不正确。

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 [email protected]

+0

'if(dados ==“email =”)'为什么多余的等号?如果你正在查看它是否空白,请执行if(dados ==“”)或if($ var.val()=='')' –

+0

@ Fred-ii-他们没有试图检查如果查询字符串是空白的。你的第二个建议是更好的。 – 4castle

+0

@ 4castle我已经看到评论'//>>>这个字段,如何检查字段是空白的?'部分告诉我他们正在检查它是否留空白/空。这就是我写这个的原因。 –

回答

0

我觉得你的代码试图通过AJAX提交表单之前验证电子邮件。如果是这样的话,这个代码对于我来说几乎没有问题。

return false在提交调用结束时可能无法在Firefox上运行。使用e.preventDefault();。看看this的帖子。如果您在Chrome上尝试此代码,则可能会失败,因为您没有return true任何地方。

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

if(dados == "email=")可以更改为if (email.val() != '')。所以你也不需要爸爸。

你不使用myForm变量无处。它应该被删除。

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

+1

'return false;'在jQuery中总是表现一致。它只在普通的JavaScript中表现不一致。没有必要担心它。如果你想知道Firefox中发生了什么,[看这里](http://stackoverflow.com/questions/5422770/returning-false-from-click-handler-doesnt-work-in-firefox)。 – 4castle

0

返回的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错误,做错误error回调的AJAX功能内处理: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'