使用 PHP 和 Prototype 成功进行 AJAX 验证后无法获取要提交的表单
我的网站上有由 PHP 处理的表单,包括验证。通常,如果出现错误,用户将返回到带有相应错误消息的表单。对于启用了 javascript 的用户,我尝试使用 Prototype 编写一个 javascript,它使用 AJAX 执行相同的 PHP 验证(以避免代码重复)并显示错误消息,而无需实际提交表单并等待其重新加载。
该脚本非常适合通过 AJAX 捕获错误并显示它们。但是,当没有错误时,我无法提交表单并前进到下一页。我一直在使用 Event.stopObserving,但它只是重新加载页面(这不是表单操作 url,表单提交到与表单本身不同的页面,因此不是服务器将浏览器发送回来)。我不太擅长 javascript 和 Prototype,所以我可能错过了一些明显的东西,但任何帮助将不胜感激。无论如何,这是我的代码:
document.observe("dom:loaded", function() {
$$('form')[0].observe('submit', validate);
function validate(event) {
event.stop();
// remove any existing error messages
var curErrors = $$('ul.error');
for( i=0, im=curErrors.length; i<im; i++ ) curErrors[i].remove();
$$('form')[0].request({
parameters: { 'js' : 1 },
requestHeaders: {Accept: 'application/json'},
onSuccess: function(req) {
var errors = req.responseText;
if( errors == '[]' ) {
// no errors, submit form to server
$$('form')[0].stopObserving('submit', validate);
$$('form')[0].submit();
} else {
// have errors, display error messages
var errors = errors.evalJSON(true);
for( var error in errors ) {
var errorMsg = '<ul class="error"><li>' + errors[error] + '</li></ul>';
var input = $$('[name="'+error+'"]')[0];
// display error message next to form field
. . .
}
}
},
onFailure: function() {
// can't validate here, let server do it
$$('form')[0].stopObserving('submit', validate);
$$('form')[0].submit();
}
});
}
});
I have forms on a site that are processed by PHP, including validation. Normally, if there's errors, the user is returned to the form with the appropriate error messages. For users with javascript enabled, I'm trying to write a javascript with Prototype that uses AJAX to do the same PHP validation (to avoid code duplication) and display the error messages without having to actually submit the form and wait for it to reload.
The script works very well at grabbing the errors via AJAX and displaying them. But, when there are no errors, I cannot get the form to submit and move forward to the next page. I've been working with Event.stopObserving, but it just reloads the page (which is not the form action url, the form submits to a different page than the form itself, so it's not the server sending the browser back). I'm not that good with javascript and Prototype, so I'm probably missing something obvious, but any help would be greatly appreciated. Anyway, here is my code:
document.observe("dom:loaded", function() {
$('form')[0].observe('submit', validate);
function validate(event) {
event.stop();
// remove any existing error messages
var curErrors = $('ul.error');
for( i=0, im=curErrors.length; i<im; i++ ) curErrors[i].remove();
$('form')[0].request({
parameters: { 'js' : 1 },
requestHeaders: {Accept: 'application/json'},
onSuccess: function(req) {
var errors = req.responseText;
if( errors == '[]' ) {
// no errors, submit form to server
$('form')[0].stopObserving('submit', validate);
$('form')[0].submit();
} else {
// have errors, display error messages
var errors = errors.evalJSON(true);
for( var error in errors ) {
var errorMsg = '<ul class="error"><li>' + errors[error] + '</li></ul>';
var input = $('[name="'+error+'"]')[0];
// display error message next to form field
. . .
}
}
},
onFailure: function() {
// can't validate here, let server do it
$('form')[0].stopObserving('submit', validate);
$('form')[0].submit();
}
});
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要在验证方法中立即调用
event.stop()
- 事件处理程序将在事件发生之前发生,这意味着验证方法将在表单运行之前运行。已提交。只需将该行移动到有关验证失败的 if 语句的分支内,它应该可以正常工作。http://www.prototypejs.org/api/event/stop
Don't call
event.stop()
immediately within your validate method - the event handler will occur before the event does, meaning that the validate method will run before the form is submitted. Simply move that line within the branch of the if statement regarding validation failure, and it ought to work just fine.http://www.prototypejs.org/api/event/stop