简体   繁体   中英

How to show comment posted instantly after submitting

I am using ISSET condition to add comment on the page after saving it to database.

if (ISSET($_POST['comment_submit'])) {

    $comment_author = $_POST['comment_author'];
    $comment_content = $_POST['comment_content'];
    $post_id = $_POST['post_id'];

    if ($comment_content!="") {
        $new_comment = mysql_query("INSERT INTO comment (post_id, comment_author, comment_content, comment_date) VALUES ('$post_id', '$comment_author','$comment_content', now())");
    }
}

but after redirecting to same page its not showing comment posted recently. On refersh it starts displaying.

将在数据库中插入数据的代码放在插入查询之后。

If you are redirecting the user to same page, you won't get values in $_POST variable, since the form is not submitted again. You may have to store comment_author and comment_content values in session or you may have to pass these values while refreshing the page.

it seems that you're using pure php only.

  • first thing you must do is to have a query that list down the available comments inside your db.
  • second is posting your data and saving it.
  • third, redirect your page to the comment section you're viewing.. use

header("Location: url")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM