3

I am trying to display an alert box before redirecting to another page, here is my code, when I remove the header function it works properly but when it is here it will just redirect to the page without showing the alert box.

<html>
    <body>
        <?php
        include("dbconfig.php");

        $tempid = mysqli_real_escape_string($dbconfig, $_POST['tempid']);

        $sql_query = "DELETE FROM Visits
    WHERE visitid = '$tempid'";
        $result = Mysqli_query($dbconfig, $sql_query);
        if ($result) {

            echo '<script language="javascript">';
            echo 'alert("visit deleted successfully")';
            echo '</script>';

            header("location:../SearchCountry/search.php");
        }
        ?>
    </body>
</html>
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Xtrem fabz
  • 121
  • 3
  • 12

2 Answers2

6

PHP is executed at the server side. It renders HTML/JS/CSS and sends it to the web browser, the web browser then parses and executes the JavaScript (In your case, show the alert dialog.)

However, once you call

header ("location:../SearchCountry/search.php");

The browser will be informed to redirect the user to ../SearchCountry/search.php immediately, without a chance to parse and execute the JavaScript. That's why the dialog will not show up.

Solution: redirect your user to another page with JavaScript instead of PHP.

 <html>
 <?php
 include("dbconfig.php");

 $tempid = mysqli_real_escape_string($dbconfig,$_POST['tempid']);

$sql_query = "DELETE FROM Visits
WHERE visitid = '$tempid'";
$result = Mysqli_query($dbconfig,$sql_query);
if($result){
    echo '<script language="javascript">';
    echo 'alert("visit deleted successfully");\n';
    echo 'window.location.href="../SearchCountry/search.php"'; //Redirects the user with JavaScript
    echo '</script>';
    die(); //Stops PHP from further execution
}
 ?>
</body>
</html>
Kevin
  • 2,775
  • 4
  • 16
  • 27
5
echo "<script>
alert('visit deleted successfully');
window.location.href='SearchCountry/search.php';
</script>";

and get rid of redirect line below.

You were mixing up two different worlds.

Naresh Kumar
  • 561
  • 2
  • 15
  • big up to you my friend, it says i have to wait 7 mins to mark as answer haha since im a noob here i have no idea why, by chance do you have any idea how i could make that alert box use two buttons like "ok" and "cancel"? – Xtrem fabz May 01 '16 at 14:30
  • 1
    @Xtremfabz use confirm instead of alert =) – Naresh Kumar May 01 '16 at 14:36
  • 1
    you can use if else, as the confirm function returns true or false you can use it to control yes or no `var answer = confirm("visit deleted successfully") if (answer){ // if user click on yes window.location.href='SearchCountry/search.php'; } else{ //if user click on no document.write("sorry"); }` – Naresh Kumar May 01 '16 at 15:02
  • Answers with explanations are always good, too. (See Kevin's answer below, for a fuller description of what is happening.) – HoldOffHunger Aug 28 '18 at 17:15