请尝试通过使用Ajax同时提交和验证我的表单到我的数据库,但它不适合我.
这是我的jquery
$(document).ready(function(){
$(".button").click(function(){
$("#myform").validate();
//Ajax to process the form
$.ajax({
type: "POST",
url: "process.php",
data: { firstname: $("#firstname").val()},
success: function(){
$('#message').html(data);
}
});
return false;
});
});
问题是当我提交表单时,Ajax表单提交给自己.
请一起使用jquery validate和$.ajax的正确方法是什么?
解决方法:
试试这个(按预期为我工作):
HTML表格:
// JQuery Script to submit Form
$(document).ready(function () {
$("#commentForm").validate({
submitHandler : function () {
// your function if, validate is success
$.ajax({
type : "POST",
url : "process.php",
data : $('#commentForm').serialize(),
success : function (data) {
$('#message').html(data);
}
});
}
});
});
Name (required, at least 2 characters)
E-Mail (required)
URL (optional)
Your comment (required)
PHP代码:
echo $_POST['email'];
?>
标签:jquery,php,jquery-validate
来源: https://codeday.me/bug/20190716/1478694.html