Open In App

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

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.

Syntax:



<?php
     session_start();
     session_register('variable_name');
     $_SESSION['variable_name']=variable_value;
?>

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.

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.


Article Tags :