DEV Community

loading...
Cover image for How to pass variable value from one page to another in PHP?

How to pass variable value from one page to another in PHP?

joshuabrown0010 profile image joshua-brown0010 ・2 min read

Syntax:

     session_start();
     session_register('variable_name');
     $_SESSION['variable_name']=variable_value;
?>
Enter fullscreen mode Exit fullscreen mode

Step 1

Begin your localhost server as Apache, etc. Finish writing the HTML tags and write the beneath code within the BODY section. Then save the file with the format ‘form1.php’ in the local directory of your localhost. Later on, in your web browser type localhost address followed by ‘\form1.php’

<form method="POST" action="form2.php"> 
    <pre>Name: <input type="text"
        name="user_name"> 
    </pre> 

    <pre>Email Address: <input type="text"
        name="user_email_address"> 
    </pre> 

    <pre>Mobile Number: <input type="number"
        name="user_mobile_number"> 
    </pre> 

    <input type="submit" value="Next"> 
</form> 
Enter fullscreen mode Exit fullscreen mode

Requested information will be paused to the PHP page connected with the form (action=”form2.php”) with the use of the post method. After that submitted information will be stored in the session array

Step 2

Return the process of saving the file as explained over. Remember use the file name ‘form2.php’. At the time you click ‘Next’ on form1.php page. So, this page will be requesting the college/ company name, city, state user is in and the course applicant is applying for.

Code:

<?php 

// Initialize the session 
session_start(); 

// Store the submitted data sent 
// via POST method, stored 

// Temporarily in $_POST structure. 
$_SESSION['name'] = $_POST['user_name']; 

$_SESSION['email_address'] 
        = $_POST['user_email_address']; 

$_SESSION['mobile_number'] 
        = $_POST['user_mobile_number']; 

?> 

<!-- Form for other details--> 
<form method="POST" action="form3.php"> 

    <pre> 
        Company/College: 
        <input type="text" name="college_name"> 
    </pre> 

    <pre> 
        City: 
        <input type="text" name="city"> 
    </pre> 

    <pre> 
        State: 
        <input type="text" name="state"> 
    </pre> 

    <pre> 
        You're a: 
        <input type="radio" name="profession"
                value="Student">Student 

        <input type="radio" name="profession"
                value="Working Professional"> 
                Working Professional 
    </pre> 

    <pre> 
        Course: 
        <select name="course"> 
            <option value="DSnA"> 
                Data Structures and Algorithms 
            </option> 

            <option value="Gate_test"> 
                GATE Mock Test 
            </option> 

            <option value="Mock_interview"> 
                Mock Interviews 
            </option> 

            <option value="Machine_learning"> 
                Machine Learning 
            </option> 
        </select> 
    </pre> 
    <br> 

    <pre> 
        <input type="checkbox"
            name="terms_and_conditions"> 
            Terms and Conditions 
    </pre> 
    <br> 

    <input type="submit" value="Register"> 

</form> 
Enter fullscreen mode Exit fullscreen mode

It will be redirected you to this page. Read more

Discussion (0)

pic
Editor guide