显示错误,而不需要刷新页面 [英] Showing error without page refresh
问题描述
我在做一个评论功能为我的网站,需要让这个以后人提交评论(它包含错误),它会显示错误而无需刷新页面。我几乎一无所知AJAX / JQuery的,所以我需要一些帮助。
I'm making a comment feature for my website and need to make it so that after the person submits a comment (and it contains errors), it will show the error without refreshing the page. I know almost nothing of AJAX/JQuery, so I'll need some help.
这是我到目前为止有:
<?php
if(isset($_POST['reply_submit'])) {
$reply = $_POST['new_reply'];
$reply_message = "";
if(empty($reply)) {
$reply_message = "Your comment is too short.";
}
}
?>
<html lang="en">
<body>
<form class="no-margin" method="POST" action="">
<textarea name="new_reply" placeholder="Write a reply..."></textarea>
<button name="reply_submit" class="btn post-button" type="submit">Post' . (isset($reply_message) ? '<div class="comment-warning">' . $reply_message . '</div>' : '') . '</button>
</form>
</body>
</html>
所以我需要做的是,如果人的评论中不符合标准(在这种情况下,空场),我需要它来显示这个错误行而无需刷新页面:
So what I need it to do is, if the person's comment box doesn't meet the criteria (in this case, an empty field), I need it to show this error line without refreshing the page:
<button name="reply_submit" class="btn post-button" type="submit">Post' . (isset($reply_message) ? '<div class="comment-warning">' . $reply_message . '</div>' : '') . '</button>
请帮忙。
推荐答案
有关基本验证像空字段或输入框的长度,你可以使用jQeury验证插件 - 的 http://jquery.bassistance.de/validate/demo/ 或写的东西你自己的。
For basic validations like empty fields or length of input box you can use jQeury validate plugin -- http://jquery.bassistance.de/validate/demo/ or write something of your own.
您可以使用jQuery一个更好的AJAX /错误处理。检查jQuery的AJAX的文档在这里 - http://api.jquery.com/jQuery.ajax/
You can use jQuery for a better AJAX/error handling. Check the documentation for jQuery AJAX here -- http://api.jquery.com/jQuery.ajax/
$.ajax({
url: "test.html", //URL to send the request
success: function() {
//do something. This is fired when your response is successful
},
error: function() {
//highlight the field that has error or do something else here.
}
);
希望这有助于:)
Hope this helps :)
这篇关于显示错误,而不需要刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!