kriskotoo BG
kriskotoo BG

Reputation: 321

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

//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? 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. my guess is that the reload() method resubmits the form (excuse my bad english)

Upvotes: 0

Views: 265

Answers (2)

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:

if(mysqli_num_rows($commentQuery_run) > 0) 

for first comment is false and commentsArray is empty.

Upvotes: 1

MrSmile
MrSmile

Reputation: 1233

You better should place your POST-processing code in header of file, and you will be able to use header() redirect. 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>";
                }

Upvotes: 1

Related Questions