好的,所以我在jQuery覆盖中有以下表单:
<div class="profile_about_edit_container" id="profile_about_edit_container">
<form id="profile_edit_form" name="profile_edit_form" method="post" action="validation.php">
<label>First Name:</label>
<input type="text" name="firstname" maxlength="50" size="30">
<label>Last Name:</label>
<input type="text" name="lastname" maxlength="50" size="30">
<button type="submit" class="save">Save</button>
<button class="close">Cancel</button>
</form>
</div>
这是使用class="profile_popups_form"的<a>
和以下Javascript显示的:
$(document).ready(function() {
$(".profile_popups_form").overlay({
});
});
这显示正确,validation.php然后返回一系列错误消息,如下所示:
if (count($errors) > 0) {
echo json_encode($errors);
}
但现在我正在尝试使用jQuery客户端&此表单上的服务器验证。
我试过这个:
$(document).ready(function(){
var form = $("#profile_edit_form");
$("#profile_edit_form").submit(function(e) {
e.preventDefault();
var input = $("#profile_edit_form").validator();
$.getJSON(form.attr("action") + "?" + form.serialize(), function(json) {
if (json !== '') {
input.data("validator").invalidate(json);
}
else
$('#profile_edit_form').unbind('submit').submit();
});
});
目的是以jQuery Tools Validation的正常方式提交表单并显示此错误消息数组。但我运气不好。
我是不是正在接近这个位置?如果是,我做错了什么?我不确定是Javascript导致了这个问题,还是我在逻辑上正确地处理了这个问题。我几乎找不到任何资源来解释如何成功地将JQueryToolsValidation与PHP一起使用。
目前,数组只是显示在屏幕上,就好像你回显了文本一样。
我使用以下资源来获取返回数组的代码:http://www.abdullahyahya.com/2012/06/20/javascript-client-side-form-validation-using-server-side-form-validation/
尝试对php文件执行ajax请求,然后从服务器返回响应。客户端可以通过各种方式完成;从HTML5标签到纯正则表达式
data = $(this).serializeArray();
//we serialized the data of the form and then we do the ajax request
$.ajax({
url: 'validate.php',
type: 'post',
data: data,
dataType : 'json',
success: function (dat) {
if(dat.error==0){
//dat.error is my returned error from the php file
}else{
//handle the errors
}
}
},
error: function(){
//that's if something is wrong with sent data
alert('failure');
}
});