All TalkersCode Topics

Follow TalkersCode On Social Media

How To Redirect To Another Page In PHP

Last Updated : Jul 1, 2023

How To Redirect To Another Page In PHP

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.

if (isset($_POST['submit'])) {
    $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!";
    } else {
        echo "Failed To Send Mail";
<!DOCTYPE html>
<html lang="en">
    <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">
    <div class="main">
        <div class="inner">
            <div class="welcome">
                Student's Registration Form
            <div class="registration_options">
                <form action="" method="post">
                    <div class="rleft_body">
                        <label for="">
                            Enter your name here
                        <label for="">
                            Enter your email here
                        <label for="">
                            Enter Student's phone number
                        <label for="">
                            Enter your subject here
                        <label for="">
                            Type your message here
                    <div class="rright_body">
                        <input type="text" name="name" id="">
                        <input type="email" name="email" id="">
                        <input type="tel" name="nmbr" id="">
                        <input type="text" name="subject" id="">
                        <textarea name="body" rows="5" cols="40" placeholder="Type your message here..."></textarea>
                    <div class="registration_submit">
                        <input type="submit" value="Submit" id="save" name="button">
  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. Thirdly, <body> tag is used to define the webpage body. All the contents to show on website are written here.
  5. 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.
  6. 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.
  7. 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.
  8. 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.

Latest Tutorials