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

时间:2018-07-05 14:40:45

标签: javascript php html forms page-refresh

这可能非常简单,但是对于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

每次我提交表格时,都会出现“请不要重复自己/其他用户”错误。为什么? window.location.reload()函数还会重新提交表单吗?还是我做错了什么?有没有更好的方法可以重新加载网站?很显然,我需要重新加载页面,以便显示新评论。再次,对php / js / html来说,我真的是新手,所以请解释为什么我的代码无法按预期的方式工作。我的猜测是reload()方法重新提交表单(对不起,我的英语不好)

2 个答案:

答案 0 :(得分:1)

最好将POST处理代码放在文件的标头中,这样您就可以使用header()重定向了。要显示错误,可以使用一些标志;看到:

                // 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';
                    }
                }

然后在旧位置(页面中间)显示错误:

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>";
                }

答案 1 :(得分:1)

  

每次我提交表格时,都会出现“请不要重复自己/其他用户”错误。为什么?

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

第一条评论为true,因为:

if(mysqli_num_rows($commentQuery_run) > 0) 

第一个评论为false,而commentsArray为空。