2017-02-28 58 views
0

我试图在查询执行后立即刷新页面。该页面能够显示弹出消息“成功”。但是,它无法正确刷新页面,只能无限显示弹出消息。当前网页的网址是http://localhost/test/test.php?post=1PHP刷新页面有'?var = value'

<form method="post" role="form" action="reply.php"> 
    <fieldset> 
    <div class="form-group"> 
    <textarea name="reply" class="form-control" rows="3" placeholder="Comment" required autofocus=""></textarea> 
    </div>   
    <button name="post" type="submit" class="[ btn btn-success ]" data-loading-text="Loading...">Post reply</button> 
     </fieldset> 
</form> 

reply.php

if (isset($_POST['post'])) {    
       $description =$_POST['reply']; 
       $stmt = "INSERT INTO reply (comments) VALUES (:description)"; 
       $p = $MySQLi_CON -> prepare($stmt); 

       $results = $p -> execute(array(
       ":description" => $description 
       )); 
       echo '<script language = "javascript">'; 
       echo 'alert("Successful")'; 
       echo '</script>'; 
       echo "<script> location.reload(true); </script>"; 

       if(!$results){ 
         echo '<script language = "javascript">'; 
         echo 'alert("Fail")'; 
         echo '</script>'; 
         echo "<script> location.reload(true); </script>";    
       }   
    } 
+0

JavaScript的JavaScript头?只需使用:header('location:URL to same page');或者你可以使用jQuery与Ajax,这也是一个更好的解决方案,然后在PHP中使用JavaScript! – Soheyl

+0

我试图找到相同的页面,它是http://localhost/test/test.php?post = 1。如果我尝试了这种方法,它只会去http://localhost/test/test.php – fypforstack

+0

谁说的?!你可以在标题的URL中使用任何你想要的东西!只需使用:header('location:localhost/test/test.php?post = 1'); – Soheyl

回答

4

你为什么不使用使用此代码

<?php 
if (isset($_POST['post'])) { 
      $description =$_POST['reply']; 
      $stmt = "INSERT INTO reply (comments) VALUES (:description)"; 
      $p = $MySQLi_CON -> prepare($stmt); 

      $results = $p -> execute(array(
      ":description" => $description 
      )); 
     header('Location: http://localhost/test/test.php?post=1'); exit(); 
} 
?> 

您也可以使用此代码

<?php 
if (isset($_POST['post'])) { 
$description = $_POST['reply']; 
$stmt = "INSERT INTO reply (comments) VALUES (:description)"; 
$p = $MySQLi_CON->prepare($stmt); 

$results = $p->execute(array(
    ":description" => $description 
)); 
header("Location: " . $_SERVER['HTTP_REFERER']); 
exit(); 
} 
?> 
+0

我试过header('Location:http://localhost/test/test.php '),但test.php?post = 1是'?var = value'。该URL不是固定的。 1是post_ID – fypforstack

+1

@fypforstack的价值,这基本上是我说的:) – Soheyl

+0

@soheyl 但我可能有例如。我只想刷新当前页面我在 test.php?post = 1 test.php?post = 2 test.php?post = 3 – fypforstack