Change language

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

Syntax:
 
Example.This example illustrates the steps to create a three-page form using PHP and browser sessions. This applies to the registration form of the coaching institute. On the first page of the form, the user will be prompted to enter their name, email address and mobile number, which will be carried over to another PHP page. Where information will be stored in session directories.
  • Code 1:Start your local server like Apache, etc. Finish writing your HTML tags and write the following below is the code in the BODY section. Save the file in "form1.php"format in the local directory of your localhost. Open a web browser and enter your local address and then "/form1.php" . < form method = "POST" action = "form2.php" >
     Name:   "text"  name =  "user_name"  >   
     Email Address:  " text "  name =  "user_email_address"  >   
     Mobile Number:  " number "  name =  "user_mobile_number"  >   
    " submit " value = " Next " > < / form >
  • Output:it will open your the form as follows, the requested information will be sent to the PHP page associated with the form (action = ”form2.php”)using the POST method. In the next step, the provided information will be saved in the session array. 
  • Code 2:Repeat the file save process as described above. Use the file name "form2.php". When you click "Next" on the form1.php page. This page will ask for the name of the college / company, city, state the user is in, and the course he / she is applying for.  
    // Initialize the session
    session_start();  
    // Save the submitted data
    // via POST method, saved 
    // Temporarily in the $_POST structure. $_ SESSION [ ’name’ ] = $_ POST [ ’user_name’ ]; $_ SESSION [ ’ email_address’ ] = $_ POST [ ’user_email_address’ ]; $_ SESSION [ ’ mobile_number’ ] = $_ POST [ ’user_mobile_number’ ];  
    ?> for other details -> < form method = "POST" action = "form3.php" >
       Company / College:     "text"  name =  "college_name"  >   
       City:    " text "  name =  "city"  >   
       State:    " text "  name =  "state"  >   
       You’re a:     "radio"  name =  "profession"  value =  "Student"  > Student     "radio"  name =  "profession"  value = " Working Professional "  >   Working Professional   
       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 >   

         " checkbox "  name = " terms_and_conditions "  >   Terms  and  Conditions   

    " submit " value = " Register " >  
    < / form >
  • Output:it will be redirected to this page, which will look like this:
  • Code 3:In this step, we will extract information from the session array and store it in our MySQL database. Create a third file named form3.php and write the following code in the BODY section and apply the required HTML tags. // Session initialization session_start(); // write MySQL Query to insert details $insert_query = ’insert into subscriptions ( name, email_address, mobile_number, college_name, city, state, profession, course, terms_and_conditions, ) values ​​( ’. $_SESSION [’ name’]. ", ". $_SESSION [’email_address’]. " , ". $_SESSION [’mobile_number’]. " , ". $_POST [’college_name’]. " , ". $_POST [’city’]. " , ". $_POST [’state’]. " , ". $_POST [’profession’]. " , ". $_POST [’course’]. " , ". $_POST [’terms_and_conditions’]. " );" // run the query mysql_query ( $insert_query ); ?>
     Successfully Registered 
  • Output:by clicking the " Register ”on page 2, it will redirect you to this page where your data will be sent to the database. As a prerequisite, you must link your page to a MySQL database. For this you can refer to this
Output: sessions can be used to keep form data active until the browser is closed or the site is left. Please note that when writing the final request, we used the data from the $_SESSION array, as well as the data from the $_POST array, which was posted in the last step of the form.

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method