简体   繁体   English

刷新页面而不重新提交表单

[英]Refresh page without form resubmit

this is probably very simple but im really new to php and js i made a comment system for my site but i have an issue that i cant figure out 这可能很简单,但是对于php和js来说我真的很新,我为自己的网站创建了一个注释系统,但是我有一个我无法弄清楚的问题

//comment section
$commentsArray = array();

$commentQuery_run = mysqli_query($con, "SELECT * FROM comments WHERE PostID='$userPostId'");
if (mysqli_num_rows($commentQuery_run) > 0) {
  echo "<b id='commcount'>Comments:".mysqli_num_rows($commentQuery_run).
  "</b>";
  while ($commentRow = mysqli_fetch_assoc($commentQuery_run)) {
    $commentID = $commentRow['id'];
    $commentUsername = $commentRow['username'];
    $commentUserPfpPath = $commentRow['path'];
    $commentContent = $commentRow['text'];
    $commentDate = $commentRow['date'];
    $commentsArray[] = $commentContent;

    echo "html for displaying the comments";
  }
} else {
  echo "<b id='commcount'>No comments! Be the first one to comment!</b>";
}

if ($isLoggedIn === true) {
  echo "<form id='commForm' method='POST' action=''> <
    input id = 'commTextInp'
  type = 'text'
  placeholder = 'Your comment...'
  name = 'commentText' > < br >
    <
    input id = 'commSubmInp'
  type = 'submit'
  name = 'commentSubmit'
  value = 'Post Comment' >
    <
    /form>";
} else {
  echo "<b id='commcount'>Please Login In to comment!</b>";
}
//comment section

//coment process
if (isset($_POST['commentSubmit'])) {
  if (isset($_POST['commentText']) && !empty($_POST['commentText'])) {

    $postCommentUsername = $_SESSION['username'];
    $postCommentPfpImg = $_SESSION['pfpimg'];
    $postCommentContents = mysqli_real_escape_string($con, htmlentities($_POST['commentText'], ENT_QUOTES));
    $postCommentDate = date("d/m/Y H:i");

    if (!in_array($postCommentContents, $commentsArray)) {
      $postCommentQuery_run = mysqli_query($con, "INSERT INTO comments VALUES('','$userPostId','$postCommentUsername','$postCommentPfpImg','$postCommentContents','$postCommentDate')");

      if ($postCommentQuery_run === true) {
        echo "<script> window.location.reload() </script>";
      } else {
        echo "<b style='color:red;'>Error while submitting comment!</b>";
      }
    } else {
      echo "<b style='color:red;'>Please don't repeat yourself/other users!</b>";
    }
  } else {
    echo "<b style='color:red;'>Please write something in your comment and try again</b>";
  }
}
echo "</center>";
//comment process

every time i submit the form i get the "please dont repeat yourself/other users" error. 每次我提交表单时,都会出现“请不要重复自己/其他用户”错误。 why? 为什么? does the window.location.reload() function also re-submit the form? window.location.reload()函数还会重新提交表单吗? or im I doing something completely wrong? 还是我做错了什么? and is there any better method for reloading the site? 有没有更好的方法可以重新加载网站? as it might be obvious i need to reload the page so that the new comment shows up. 很显然,我需要重新加载页面,以便显示新评论。 again, im really new to php/js/html so please explain why my code isnt working the way its supposed to. 再次,对php / js / html来说,我真的是新手,所以请解释为什么我的代码无法按预期的方式工作。 my guess is that the reload() method resubmits the form (excuse my bad english) 我的猜测是reload()方法重新提交表单(对不起,我的英语不好)

You better should place your POST-processing code in header of file, and you will be able to use header() redirect. 您最好将POST处理代码放在文件的标头中,这样便可以使用header()重定向。 To show error, you can use some flag; 要显示错误,可以使用一些标志; see: 看到:

                // here we store all our comments
                $commentsArray = [];

                $commentQuery_run = mysqli_query($con,"SELECT * FROM comments WHERE PostID='$userPostId'");
                while($commentRow = mysqli_fetch_assoc($commentQuery_run)){
                   $commentsArray[] = $commentRow;
                }

              //coment process
                if(isset($_POST['commentSubmit'])){
                    if(isset($_POST['commentText']) && !empty($_POST['commentText'])){

                        $postCommentUsername = $_SESSION['username'];
                        $postCommentPfpImg = $_SESSION['pfpimg'];
                        $postCommentContents = mysqli_real_escape_string($con, htmlentities($_POST['commentText'], ENT_QUOTES));
                        $postCommentDate = date("d/m/Y H:i");

                        if(! array_search($postCommentContents, array_column($commentsArray, 'text')) ){
                            $postCommentQuery_run = mysqli_query($con,"INSERT INTO comments VALUES('','$userPostId','$postCommentUsername','$postCommentPfpImg','$postCommentContents','$postCommentDate')");

                            if($postCommentQuery_run === true){
                                header("Location: " . $_SERVER['PHP_SELF']);
                            }
                            else {
                                $is_error = 'ERROR';
                            }
                        }
                        else{
                            $is_error = 'DUPLICATE';
                        }
                    }
                    else{
                        $is_error = 'NO_DATA';
                    }
                }

and next, in the old place (in the middle of page) you can show error: 接下来,在旧位置(页面中间),您可以显示错误:

if(isset($is_error)) {
    switch($is_error) {
         case 'DUPLICATE':
             echo "<b style='color:red;'>Please don't repeat yourself/other users!</b>";
             break;
         case 'NO_DATA': 
             echo "<b style='color:red;'>Please write something in your comment and try again</b>";
             break;
         default: 
             echo "<b style='color:red;'>Error while submitting comment!</b>";
    }
}

// ...........

                // PRINT ALL COMMENTS HERE
                if(count($commentsArray)>0){
                    echo "<b id='commcount'>Comments:" . count($commentsArray) . "</b>";
                    foreach($commentsArray as $comment){

                        // $comment contains all your db-fields
                        echo "html for displaying the comments";
                    }
                }
                else{
                    echo "<b id='commcount'>No comments! Be the first one to comment!</b>";
                }

every time i submit the form i get the "please dont repeat yourself/other users" error. 每次我提交表单时,都会出现“请不要重复自己/其他用户”错误。 why? 为什么?

if(! in_array($postCommentContents,    $commentsArray)) 

for first comment is true because: 第一条评论是true因为:

if(mysqli_num_rows($commentQuery_run) > 0) 

for first comment is false and commentsArray is empty. 对于第一个注释为falsecommentsArray为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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