kobi
kobi

Reputation: 184

Java script alert doesn't work in my php file?

$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)

Upvotes: 0

Views: 237

Answers (2)

Alex Howansky
Alex Howansky

Reputation: 53533

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.

Upvotes: 1

JeanPaul98
JeanPaul98

Reputation: 492

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

Upvotes: 1

Related Questions