Jump to content

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.