-2

My PHP code is redirecting to the provided page without showing the alert box even though I have provided it in the code. Here is the code :

echo "<script type='text/javascript'>alert('Registered Successfully.')</script>";
header("Location:/magz/index - Copy.php");
Dharman
  • 30,962
  • 25
  • 85
  • 135
sundeep
  • 27
  • 1
  • 7
  • You're outputting before header btw http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php and error reporting would have told you so http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Feb 28 '16 at 18:41

1 Answers1

0

the php gets executed before it hits the browser. the JavaScript is executed after the PHP. In this case since PHP is redirecting the page, the JavaScript is never executed.

Solution

if($result == 1)
{
    echo "<script type='text/javascript'>alert('Registered Successfully.');
    window.location.assign(\"/magz/index - Copy.php\");</script>";
}

In this case, you are making JavaScript handle the redirect.

Josh S.
  • 612
  • 3
  • 8