fypforstack fypforstack -4 years ago 105
PHP Question

PHP refresh a page which has '?var=value'

I am trying to refresh the page right after the query is executed. The page was able to display the pop-up message "Successful". However, it was not able to refresh the page properly and only display pop-up message infinitely. The current page url is

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

Answer Source

you can use this code

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

you can also use this code

<?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();
 }
?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download