1

I am new to php . and i am trying to pop up an alert box before the page action = "newpage.php" gets executed using the header function , but it isn't working and every time without the alert box being popped up, i get redirected to the next page on successful form submissions.

what should i do to pop an alert box on successful submission of data in my database before going to the next page

here's my code snippet that i have used to do so---->

if($conn->query($insertQuery)){
   if($_POST) {
         echo '<script>  alert("Registered");   </script>';
   }
   else{
         echo '<script>  alert("Not");   </script>';
   }
   header("location:login.php");
Ajinkya P
  • 23
  • 3
  • 1
    You have to realize the page lifecycle. PHP code executes before the browser renders the page. What you want to do is impossible. – epascarello Jun 03 '22 at 13:50
  • 1
    All `header()`-calls _must_ be called before _any_ output what so ever. Also, all PHP code will be executed on the server before the results are returned to the client, which is where the JS code gets executed. What you could do is submit the data to PHP using Ajax (make JavaScript submit the data in the background, using something like [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)). Your PHP code then just returns the status (if it was successful or not). – M. Eriksson Jun 03 '22 at 13:50
  • 1
    I recommend that you read [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming). Keep in mind that PHP is server side while JS being client side when reading it. – M. Eriksson Jun 03 '22 at 13:54

1 Answers1

0
if($conn->query($insertQuery)){
   if($_POST) {
         echo '<script>  alert("Registered");   </script>';
   }
   else{
         echo '<script>  alert("Not");   </script>';
   }
echo '<script> window.location.href = "login.php";   </script>';

Try This

Uttam Nath
  • 644
  • 4
  • 16