简体   繁体   English

jQuery通过对外部php文件的ajax调用来验证表单,以检查所有字段并阻止表单提交(如果存在错误消息)

[英]jquery validate form with ajax call to external php file that checks all fields and block form submit if there is error message

I want to validate form field on submit event. 我想在提交事件时验证表单字段。 I am sending ajax request to php file that checks the fields and returns error message or nothing if all is OK. 我将ajax请求发送到php文件,该文件检查字段并返回错误消息,或者如果一切正常,则不返回任何内容。 Here is the js code I have: 这是我拥有的js代码:

$('form.p_form').submit(function (){
var description = $.trim($('#f9').val());
//this blocks form submit if empty field and it works
if(description === '') return false;
//this doesnt work
if(description !== ''){
var aa = $.post("checkdescription.php",{
     description: description
 },
 function(data, status){
    if(data !== ''){
    return data;
    }
 });

 //here I need to know the response text returned from ajax so I can say like this:
 if(aa === 'error message'){
 return false;
    }
 }
});

Please help, I am stacked and I am posting this question for second time as I could not get help, Thanks in advance ! 请帮助,我堆积如山,由于无法获得帮助,我第二次发布此问题,在此先谢谢您!

Your variable aa will not contain any data that you are expecting (in fact it may contain the promise returned from calling the post method of AJAX). 您的变量aa将不包含您期望的任何数据(实际上,它可能包含通过调用AJAX的post方法返回的承诺)。

Moreover you're using the default synchronous type of ajax call with the post method . 此外,您将post方法使用默认的ajax调用同步类型

In order to have access to the returned data (even outside of the promise) you may use $.ajax with the property async: false as such: 为了访问返回的数据(甚至不在promise中),您可以将$.ajaxasync: false属性一起使用:

 var aa = ''; //the result
 $.ajax({
     url: "checkdescription.php",
     async: false,
     data: {
     description: description
     }, 
     success: function(data, status){
         aa = data;
      }
});

Then, you can check the value of the variable aa soon after the ajax call. 然后,您可以在ajax调用后不久检查变量aa的值。

 if(aa === 'error message'){
     return false;
 }

You're trying to compare the response synchronously with an asynchronous get call. 您正在尝试将响应与异步get调用进行同步比较。 The value of var aa won't be set by the callback function until after the ajax call has completed. 直到ajax调用完成后,回调函数才能设置var aa的值。 You have to keep that in mind when trying to compare synchronous vs asynchronous. 在尝试比较同步与异步时,必须牢记这一点。

So let's go step-by-step what would happen with your code here: 因此,让我们一步一步地了解您的代码将在此处发生的情况:

  1. Form submit 表格提交
  2. [Presumption] description isn't blank [推定]说明不是空白
  3. var aa = undefined var aa =未定义
  4. Ajax post is called to checkdescription.php Ajax发布被称为checkdescription.php
  5. aa does not equal "error message" aa不等于“错误消息”

aa will never equal 'error message' and hits the end of your submit function (because the value of aa still hasn't been set yet by the completed ajax response aa永远不会等于'error message'并且会到达您的submit函数的结尾(因为完成的ajax响应尚未设置aa的值

  1. (This will probably never in this code, but let's pretend it could) The ajax call returns a response and then your call back function would be instantiated and it would either return data or false. (这可能永远不会在这段代码中,但是让我们假装可能)ajax调用返回一个响应,然后您的回调函数将被实例化,或者返回数据,或者返回false。 Now var aa has a value. 现在var aa有了值。

Script should catch response in 'data' variable of post request. 脚本应在请求后的“ data”变量中捕获响应。 ie either empty on success or 'error message' on error. 即成功时为空或错误时为“错误消息”。 Please try below and check if this works for you. 请在下面尝试并检查是否适合您。

var aa = $.post("checkdescription.php",{
 description: description
 },
 function(data, status){
  // data will be empty is everything is ok and "error message" if there is validation error
   if(data == 'error message'){
       // handle error
   } else {
      // on success it should be here
   }
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM