Open In App
Related Articles

How to pass form variables from one page to other page in PHP ?

Improve Article
Save Article
Like Article

Form is an HTML element used to collect information from the user in a sequential and organized manner. This information can be sent to the back-end services if required by them, or it can also be stored in a database using DBMS like MySQL. Splitting a form into multiple steps or pages allow better data handling and layering of information. This can be achieved by creating browser sessions. HTML sessions are a collection of variables that can be used to maintain the state of the form attributes while the user switched between the pages of the current domain. Session entries will be deleted as soon as the user closes the browser or leaves the site.



Example: This example will illustrate the steps to create a three-paged form using PHP and Browser Sessions. It is in reference to a coaching institute’s registration form. The first page of the form will be asking the user to enter their name, email and mobile number, which will be transferred to another PHP page. Where the information will be stored into session directories.

  • Code 1: Start your localhost server like Apache, etc. Complete writing the HTML tags and write the below code inside the BODY section. Save the file with the format ‘form1.php’ in the local directory of your localhost. Open your web browser and type your localhost address followed by ‘\form1.php’.

    <form method="POST" action="form2.php">
        <pre>Name: <input type="text"
        <pre>Email Address: <input type="text"
        <pre>Mobile Number: <input type="number"
        <input type="submit" value="Next">


  • Output: It will open your form like this, asked information will be passed to the PHP page linked with the form (action=”form2.php”) with the use of the POST method. In the next step, submitted information will be stored in the session array.
    Output Of First Code
  • Code 2: Repeat the process of saving the file as explained above. Use the file name ‘form2.php’. When you will click ‘Next’ on form1.php page. This page will be asking the college/company name, city, state user is in, and the course he/she is applying for.

    // Initialize the session
    // Store the submitted data sent
    // via POST method, stored 
    // Temporarily in $_POST structure.
    $_SESSION['name'] = $_POST['user_name'];
            = $_POST['user_email_address'];
            = $_POST['user_mobile_number'];
    <!-- Form for other details-->
    <form method="POST" action="form3.php">
            <input type="text" name="college_name">
            <input type="text" name="city">
            <input type="text" name="state">
            You're a: 
            <input type="radio" name="profession"
            <input type="radio" name="profession"
                    value="Working Professional">
                    Working Professional
            <select name="course">
                <option value="DSnA">
                    Data Structures and Algorithms
                <option value="Gate_test">
                    GATE Mock Test
                <option value="Mock_interview">
                    Mock Interviews
                <option value="Machine_learning">
                    Machine Learning
            <input type="checkbox" 
                Terms and Conditions 
        <input type="submit" value="Register">


  • Output: It will be redirected you to this page, which will look like this:
  • Code 3: In this step, we will be extracting the information from the session array and storing it on our MySQL Database. Make a third file named ‘form3.php’ and write the following code inside the BODY section, and apply the necessary HTML tags.

        //Initializing the session
        //writing MySQL Query to insert the details
        $insert_query = 'insert into subscriptions (
                        ) values (
                        ' . $_SESSION['name'] . ",
                        " . $_SESSION['email_address'] . ",
                        " . $_SESSION['mobile_number'] . ",
                        " . $_POST['college_name']. ",
                        " . $_POST['city']. ",
                        " . $_POST['state']. ",
                        " . $_POST['profession']. ",
                        " . $_POST['course']. ",
                        " . $_POST['terms_and_conditions']. "
        //let's run the query
    <pre>Successfully Registered</pre>


  • Output: On clicking Register on page 2, it will redirect you to this page where your data will be submitted into the database. As a prerequisite, you have to link your page to a MySQL database. For that, you may refer this
    Final Result

Conclusion: Sessions can be used to keep form data active until either the browser is closed or the site is left. Please note that while writing the final query, we have used data from the $_SESSION array, and also data from the $_POST array, that was posted from the last step of the form.

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 31 Jul, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials