All TalkersCode Topics

Follow TalkersCode On Social Media

How To Redirect To Another Page In PHP After Submit

Last Updated : Jul 1, 2023

How To Redirect To Another Page In PHP After Submit

In this tutorial we will show you the solution of how to redirect to another page in PHP after submit, here we defined form with submit button in html page when user clicks submit button in php we can easily find out by isset() method and for redirect we need to use header() function.

In header() method we need to give path information of which page need to load as a result in webpage except header() we had another option in php using window service.

Step By Step Guide On How To Redirect To Another Page In PHP After Submit :-

Here we used isset() and header() method for achieve result.

The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL.

This function returns true if the variable exists and is not NULL, otherwise it returns false.

The header is a php built-in function for sending a raw HTTP header.

The HTTP functions are those that manipulate information sent by the webserver to the client or browser before it sends any further output.

<?php
    if(isset($_POST['submit'])){
        header('Location: new_page.html');
    }
?>
<!Doctype html>
<html>
    <head>
        <title>Redirect After Submit</title>
    </head>
    <body>
        <form method="post" action="">
            <input type="submit" value="Submit" name="submit">
        </form>
    </body>
</html>
NEW_PAGE.HTML
<!DOCTYPE html>
<html>
    <body>
        <h1>Welcome To Redirected Location...</h1>
    </body>
</html>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. First we need to create html form so we defined form in html code with submit button. Here we need to confirm two things one is form whether had method attribute with ‘post’ value and then submit whether had name attribute with value.
  4. Because in php when we need to access form elements those are important then only we can do further process.
  5. In php as we seen isset method, here we checking whether submit is set when user clicks submit button if submit is set then header() method loads path of location we defined.
  6. Here we used url of new_page html file this is needs to created in same directory. So when user click submit current php file redirect to new_page html file.

Conclusion :-

In conclusion we are able to know how to redirect to another page after submit in php.

First we need to start our xampp server then we load this program on browser we can see submit button we defined when user clicks submit button in php submit variable is set to true state so it will redirect to new_page.html file and displays message ‘Welcome To Redirected Location...’on webpage.

We can also redirect to any other existing websites like hosted site or php files.

In new_page html file we can define any other forms layout or landing page anything that will depends on our own thought.

I hope this tutorial on how to redirect to another page in PHP after submit helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪