Php 在没有页面刷新的情况下在新HTML元素中显示错误
我正在尝试为我的网站创建一个评论部分,需要这样做,以便在用户提交评论(其中包含错误)后,它将在不刷新页面的情况下显示错误(一个新的HTML元素)。我对AJAX/JQuery几乎一无所知,所以我需要一些帮助 以下是我目前掌握的情况:Php 在没有页面刷新的情况下在新HTML元素中显示错误,php,javascript,ajax,jquery,Php,Javascript,Ajax,Jquery,我正在尝试为我的网站创建一个评论部分,需要这样做,以便在用户提交评论(其中包含错误)后,它将在不刷新页面的情况下显示错误(一个新的HTML元素)。我对AJAX/JQuery几乎一无所知,所以我需要一些帮助 以下是我目前掌握的情况: <?php if(isset($_POST['reply_submit'])) { $reply = $_POST['new_reply']; $reply_message = ""; if(empty($reply)) {
<?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>
邮政局'。(isset($reply_message)“.$reply_message.”:“.”
因此,我需要它做的是,如果此人的评论框不符合条件(在本例中为空字段),我需要它在不刷新页面的情况下显示此错误行:
<button name="reply_submit" class="btn post-button" type="submit">Post' . (isset($reply_message) ? '<div class="comment-warning">' . $reply_message . '</div>' : '') . '</button>
Post'。(isset($reply_message)“.$reply_message.”:“.”
请帮忙
HTML基本相同,添加提交操作
您应该使用js进行此操作,然后执行服务器端检查,该检查也会执行相同的检查,因此js检查、ajax post、php检查、js错误返回。请不要多次发布相同的问题。继续这样做将导致您根本无法提问。
<form class="no-margin" method="POST" action="" onsubmit=verify();>
<textarea name="new_reply" id="new_reply_textarea" placeholder="Write a reply..."></textarea>
...
</form>
<div class=comment-warning></div>
function verify()
{
if ($('#new_reply_textarea').is(':empty'))
{
$('.comment-warning').html("Your comment is too short.");
return false;
}
else
{
return true;
}
}