0
$data = mysqli_query ($conn,$query)or     die(mysqli_error($conn));
if($data)
{

    echo '<script language="javascript">';
    echo 'alert("YOUR REGISTRATION IS COMPLETED...")';
    echo '</script>';


    header("Location:booking.php");
}

(This is reservation.php and same folder having booking.php)

4
  • what is the HTML output of your php script? Commented Feb 2, 2018 at 19:18
  • After the submission it go to booking.php page without any alert msg.@messerbill Commented Feb 2, 2018 at 19:21
  • 1
    My bet is that you have output buffering enabled. It prevents echos from sending the output to browser, so the header gets ahead of the train. Commented Feb 2, 2018 at 19:22
  • 2
    Headers are the first thing received by the browser. The redirect fires before JS is executed. Commented Feb 2, 2018 at 19:24

2 Answers 2

1

There are many ways to simplify this but if I were to keep it to how you want it then this should do

$data = mysqli_query($conn,$query) or die(mysqli_error($conn));
if($data) {
      echo '<script type="text/javascript">';
      echo 'alert("YOUR REGISTRATION IS COMPLETED...")';
      echo '</script>';

      echo '<script type="text/javascript">';
      echo 'setTimeout(function() {';
      echo 'window.location.href = "http://stackoverflow.com"';
      echo '}, 5000); // <-- redirect after 5 seconds';
      echo '</script>';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thn @JeanPaul98
1

Basically -- you can't do that.

Browsers don't execute JS on pages that include location header redirects. (At least the ones I've tested.) It wouldn't make much sense for a browser to render the complete page, execute all the JS, and then throw it away and load the requested redirect. If your desire is to have a message display and then redirect the user after they've clicked to acknowledge it, you'll have make a form or attach a bit of custom JS to a button.

Alternatively, you might use what's commonly called a flash message. Upon successful operation, you put the message text into the user's session and then immediately redirect to the new page. The new page looks in the session, and if it contains any message, displays it.

1 Comment

Addendum to flash message: Just be sure after you use the message to display, you immediately clear it from the session so it does not show up on every page afterwards ;) I know thats common-sense behavior, but it must be said.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.