An easy way to build data Session for a form

You wonder how data of each field on a form can be refilled after you submit a page. This post will show you how to build an array of sessions that capture the input data of a user after they submit a form.

If an anonymous arrive on your web form from the street, there’s no session until they enter some values and hit Submit button to process the form.

First, you have to have a form to collect data on your website. When customer submit the form, you loop through all the $_POST variables. To make it easy, you should build an array of all field names, so you can build SESSION names using foreach loop.

 'First Name',
   'lastname' => 'Last Name',
   'companyname' => 'Company Name',
   'address' => 'Address',
   'address2' => ' Address2',
   'city' => 'City',
   'state' => 'State',
   'zip' => 'Zip/Postal Code',
   'phone' => 'Phone',
   'ext' => 'Phone Ext',
   'fax' => 'FAX',
   'email' => 'EMail Address',);

   foreach ( $fields as $name => $value ) {
      $_SESSION[$name] = $_POST[$name];
   }
?>

Then if for any reason the form can’t be processed and customer is forced to go back to the form, all data on the form is still there. You can destroy the session anytime after the form is processed successfully. Just use the codes below to reset all SESSION variables.