Jump to content

Show error without page refresh


Erik_Fischer

Recommended Posts

I'm trying to create a comment section 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 (a new HTML element) without refreshing the page. I know almost nothing of AJAX/JQuery, so I'll need some help.

Here's what I have so far:

<?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>

Please help.

Link to comment
https://forums.phpfreaks.com/topic/281587-show-error-without-page-refresh/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.