PHP刷新一个'?var = value'的页面

时间:2017-02-28 13:34:53

标签: php pdo

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

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

1 个答案:

答案 0 :(得分:3)

您可以使用此代码

<?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();
 }
?>