Q:

How to make PHP script redirect and display a popup box simultaneously when something is true/successful?

In my PHP code, I have tried to make it so that after the user successfully sends an email, it redirects back to the contact page and then displays a popup box informing the user that the email has been successfully sent: however, the program only ever runs the top line, and does not run the popup box code. How do I fix this?

<? php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: #';
$to = '#';
$subject = 'Customer Message - Online Form';
$body = "From: ".$name.
"\r\n E-Mail: ".$email.
"\r\n Message: \r\n".$message;

if (isset($_POST['submit'])) {
  if (mail($to, $subject, $body, $from)) {
    header('Location: http://www.google.com.au');
    echo "<script>alert('Message successfully sent.');</script>";
  } else {
    echo '<p>Something went wrong, go back and try again!</p>';
  }
}

?>

A:

you are redirecting using your headers, if you try to echo first and then use header to redirect you will get an error, saying the headers are allready set.
You could add the redirect to your frontend, by returning something else like
if (isset($_POST['submit'])) {
   if (mail($to, $subject, $body, $from)) {
       echo '<script>
            alert("Message successfully sent.");
            location.href="http://www.google.com.au";
      </script>';
   } else {
       echo '<p>Something went wrong, go back and try again!</p>';
   }
}

the location redirect should wait for the alert to be dismissed, before executing

