How To Redirect To Another Page In PHP
Last Updated : Mar 11, 2024
In this tutorial we will show you the solution of how to redirect to another page in PHP and we will understand how we are able to redirect to another page in PHP after submission.
Most of times we see that like in registration form, Google forms and login pages also that when we click on button after form a new page opens.
Now, today we are here to use the same concept in php and tell you how we are able to done this in php. Let’s understand.
Step By Step Guide On How To Redirect To Another Page In PHP :-
In our previous topic that is how to redirect to another page in PHP. In that article we show you that how can we redirect to another page and we use header function in that page.
And the same concept again applies here but on button. If you don’t come in touch with that article don’t worry. We repeat this concept again. Let’s see the code given below.
<?php if (isset($_POST['submit'])) { $to = "ENTER_YOUR_EMAIL_ADDRESS_HERE"; $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['nmbr']; $subject = $_POST['subject']; $body = $_POST['body']; $headers = 'From: ' . $email . "\r\n"; $body = "name : " . $name . "\r\n" . "Phone : " . $phone . "\r\n" . "Message : " . $body; if (mail($to, $subject, $body, $headers)) { echo "Mail Sent!"; header("location:demoo.html"); } else { echo "Failed To Send Mail"; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>home page</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="main"> <div class="inner"> <div class="welcome"> Student's Registration Form </div> <div class="registration_options"> <form action="" method="post"> <div class="rleft_body"> <label for=""> Enter your name here </label> <br> <label for=""> Enter your email here </label> <br> <label for=""> Enter Student's phone number </label> <br> <label for=""> Enter your subject here </label> <br> <label for=""> Type your message here </label> </div> <div class="rright_body"> <input type="text" name="name" id=""> <br><br> <input type="email" name="email" id=""> <br><br> <input type="tel" name="nmbr" id=""> <br><br> <input type="text" name="subject" id=""> <br><br><br> <textarea name="body" rows="5" cols="40" placeholder="Type your message here..."></textarea> </div> <div class="registration_submit"> <input type="submit" value="Submit" id="save" name="button"> </div> </form> </div> </div> </div> </body> </html>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- 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.
- Thirdly, <body> tag is used to define the webpage body. All the contents to show on website are written here.
- Here, we create a simple registration page with name, email, phone number, etc. And the css applied can be seen from the file attached there.
- Now, after that here is php code given above. In this code we use isset() method to submit form and when all inputs are filled then the if at last become true and form gets submitted. As you see here is header with echo in if only.
- Means that page is redirected only when all information is true else not. So, if gets true then with the help of header webpage is redirect to demoo.html webpage.
- 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 redirect to another page in php after submit. I hope this tutorial on how to redirect to another page in PHP helps you.