Show Alert or message after a header? PHP

Question:

I was creating an insert , and when sending the data I got an alert message saying that the data had been inserted successfully, but now, the page goes blank and only shows that alert.

What I did so that it would not stay stuck was redirect again with header , only this time I do not get the message or the alert.

It's very simple I guess, I put it before and after the header and it didn't work.

if ($mail->send()) 
{

  header("location:controlador.php");

  echo '<p class="alert alert-success agileits" role="alert">Captura realizada correctamente!p>';

  } else {

  echo "Mailer Error: " . $mail->ErrorInfo;
}

Answer:

to print an alert inside php do it like this:

<?php 
     echo "<script>
                alert('Mensaje');
                window.location= 'url.php'
    </script>";
?>
Scroll to Top