我是AJAX新手。
我试图提交一个表单与ajax使用以下逻辑:
//validate form
//if invalid never submit
//if valid, ajax request to get msg
//get success html if no server side errors exist. Replace sign up page with success html
//get fail message if server side errors exist, so the user can correct them (ex. invalid credit card no)
我的AJAX:
$("form").submit(function(e){
if($(this).valid()) {
$.ajax({
url: 'formAction.php',
type: 'POST',
success: function(data){
/*convert php variables to JavaScript ones*/
if(success==true){
//relpace current page with confirmation
}
if(success==false){
//print out errors and allow user to re-submit
}
}
});
}
e.preventDefault;
});
我想知道我是否在正确的轨道上与这个实现?或者这种实现是否可能?
我非常感谢任何使这个实现工作的建议。
提前感谢!
$("form").submit(function(e){
if($(this).valid()) {
$.ajax({
url: 'formAction.php',
data: $(this).serialize(), //You need to provide this, serialize may work here
type: 'POST', //Added single quote to prevent unterminated string literal
success: function(data){
//call was successful since success message was called
},//Don't forget closing curly brace on function
error: function(data){
//Specify a call back for errors
}
});
}
e.preventDefault;
});