Akash Varde
Akash Varde

Reputation: 93

How to display ALERT massage AFTER redirecting to other page using php and jscript?

Question is simple ,

I have a form submission after successful submission of form , i want to print Alert massage on next page that "Form successfully submitted !" ?

using php and jscript

Upvotes: 1

Views: 1350

Answers (2)

user6458222
user6458222

Reputation:

I learned this approach from Laravel, its called flash message. Basically you set some variable in the session (your choice in naming)

//from your originating page
<?php
$_SESSION["flash"] = array();
$_SESSION["flash"]["message"] = "Your message";
$_SESSION["flash"]["status"] = "error";
?>
<?php
//to your destination page

if(isset($_SESSION["flash"])){
  echo $_SESSION["flash"]["message"];
  //or
  echo "<script>alert('{$_SESSION["flash"]["message"]}');</script>";
  unset($_SESSION["flash"]);
}
?>

Upvotes: 1

Harish Kumar
Harish Kumar

Reputation: 348

After saving the form you can redirect user on a page with alert script using:

// PHP
header('Location: http://www.example.com/path_to_page');

Then on that page you can add js

// Js (include jquery before it)
<script>
    $(document).ready(function(){
        alert('Form successfully submitted!');
    });
</script>

If you are not using jquery then use:

<body onload="alert_call()">
    <!-- your page content -->
    <script>
        function alert_call(){
            alert('Form successfully submitted!');
        }
    </script>
</body>

Upvotes: 1

Related Questions