All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Alert In PHP And Redirect

Last Updated : Mar 11, 2024

Alert In PHP And Redirect

In this tutorial we will show you the solution of alert in PHP and redirect, today we will understand how to alert a message in PHP with concept of redirection.

In some website we see that like in website of exams, that there is save button at last and after clicking on that button a message appears in which there is text like form submitted successfully, etc.

And user is redirected to another page. In this article, we are going to understand this concept. Let see…

Step By Step Guide On Alert In PHP And Redirect :-

Here, as we have two tasks. First one is to alert a message in php and next is to redirect that page. Now, let see how to done this in php.

<!DOCTYPE html>
      <html>
        <head>
          <title> alert in php and redirect </title>
<script src=’js/jQuery.js’>
</script>
   </head>
 <body>
 <?php
 echo "<script>
  alert('$message');
 header(“Location: https://www.google.com ”);
 </script>";
// another way
function alert($message) {
     echo "<script>alert('$message');
 window.location.href=’ https://www.google.com ’
 </script>";
}
// Function call
alert("Welcome to this world of happiness");
?>
 </body>
      </html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, inside body as we can see that there we create a php tag. In which <?php is our opening tag and ?> is our closing tag.
  6. Here, as we see that to alert a message we use alert() function with the help of jQuery. In first case, we use script inside echo and then alert inside that.
  7. In next method, we create a function with alert and call it later with required parameter.
  8. Now, for redirection here are again two methods. First one by using header() and next one is by window.location.href method. We are able to use any one from them for redirection. Both are also used inside script tag. The most used method is header() function. In which there is an key named Location and we have to give value to that parameter. The value is the location of page where we want to redirect. In case of window.locatoin.href we directly give value by using equal to sign.
  9. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to alert message in php with redirection also.

I hope this tutorial on alert in PHP and redirect helps you and the steps and method mentioned above are easy to follow and implement.