3 Use the normal form to submit, but use ajax verification. This method is a bit confusing. When we submit the form, the form can be submitted even if the ajax verification does not pass. Regarding this situation, many examples on the Internet are to change the source code. , the following is a screenshot:
. This method is feasible for the current situation. This modification method itself is not recommended and may have an impact on other places. One of the things I found is when the following situation occurs:
function beforeCall(form,options){
If(window.console){
console.log("After the form submission verification is passed, the callback before Ajax submission");
};
Return true;
};
//
function ajaxValidationCallback(status,form,json,options){
If(window.console){
console.log(status);
};
If(status === true){
alert("the form is valid!");
}
};
jQuery(document).ready(function(){
jQuery("#formID").validationEngine({
ajaxFormValidation: true, //Whether to use Ajax to submit the form
ajaxFormValidationMethod: 'post', //Set the way to send data when Ajax submits
onAjaxFormComplete: ajaxValidationCallback, //Form submission, after Ajax verification is completed
onBeforeAjaxFormValidation: beforeCall //After the form submission verification is passed, the callback before Ajax submission
});
});
beforeCall This method will not be called, so it still cannot be used
4 Use ajax to verify and submit the form using ajax. This method is confusing, and the method of modifying the source code above is not easy to use.
For the third and fourth methods, the solutions are as follows:
Add the custom attribute control="userName,email" to the form tag that uses ajax verification. The attribute value is the ID of the control to be verified using ajax (multiple controls are separated by commas).
var controlId = new Array(); //Save the control ID that failed verification
var errors = new Array(); //Save the prompt message for failed verification
The idea is to get the value of the control attribute on the form tag (use commas to separate each control ID), use ajax to traverse the request, and when the verification fails, add the control ID and prompt information to controlId and errors. , and prompt information. When submitting the form, determine whether the controlId is empty. If it is not empty, the prompt information will be displayed in a loop.
function ajaxForm2Control(obj) {
//Return when there is an error message and display the error message
If(controlId.length > 0) {
alertinfo() ;
return false ;
}
If(!$(obj).validationEngine("validate")) return false; //Verify controls that do not use ajax verification (fields that are not ajax verified can be verified normally, and will be returned if they fail)
$.ajax({
Type: "POST",
URL: $(obj).attr("action"),
Data: $(obj).serialize(),
DataType: "html",
Success: function(data){
}
});
}
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn