If you want to make a form that spans multiple pages, you will have to use special functions. Normally, your page will lose all the data it has collected once a new page is opened.
Until the development of PHP, the most common way was to use cookies. A cookie is a small text file that stores information about the user on their home computer. Each time they revisit the site or move to a new page, the previously-saved information can be accessed by reading the cookie. This technique requires proficiency in JavaScript.
PHP can use hidden fields to send data back to the form using the echo() function, but this is a tedious programming project, and the coding process is very time-consuming.
Fortunately, PHP has some built-in functions that abstract all these complexities into a simple interface called sessions.
To use sessions in your form, insert the following start function into your code:
session_start();
Variables must be registered in the session to enable the features that a session provides. Use the following function to register a variable:
session_register("varname");
varname is an HTML variable name; not a PHP variable name (i.e. the variable name does not begin with a $).
The session_start() and session_register() functions must be placed before the initial <HTML> tag in your web page's code in order for them to work properly.
If you no longer wish to use a variable in the session, you will have to unregister it using the following function:
session_unregister("varname");
To end a session and remove all instances of its use - including any registered variables - use the following function:
session_destroy();
Here is an example of sessioning in a multiage form:
<form name="ptest" method="post" action="personality2.php">
<form name="ptest" method="post" action="personality3.php">
The code at the beginning starts the session and registers all variables that will be used in the survey.
<?php
session_start(); // start the session
session_register("psname"); // adds $psname to the session
session_register("Q01"); // adds $Q01 to the session
session_register("Q02");
session_register("Q03");
session_register("Q04");
session_register("Q05");
session_register("Q06");
session_register("Q07");
...
session_register("Q44");
?>
<html>
<form name="ptest" method="post" action="results.php">
The session_start() function is needed at the start of the HTML code, but it is not necessary to register or re-register any additional variables.
<?php
session_start(); // start the session
?>
<html>
session_start() function at the very beginning of the code.
<?php
session_start(); // start the session
?>
<html>
This page uses the familiar code to store data to a data file. Note that this PHP code does not need to appear before the initial <HTML> tag.
<?php
$gb=fopen("psychDB.dat","a"); // open the data file
fputs($GB,$psname."\t"); // append the following data
fputs($GB,$Q01."\t");
fputs($GB,$Q02."\t");
fputs($GB,$Q03."\t");
....
fputs($GB,$Q44."\n"); // finish the entry with a hard return
?>
It is necessary to close the session at the end of the last page. If the session remains open, any person that visits the page from the same terminal will maintain the same variable values as the first person. As a result, the same information will be posted and reposted to the data file.
<?php
session_destroy(); // destroy session so someone else can fill it out
?>
Though not required, it is recommended that you add some code to allow users to send feedback about the survey and to allow them to report any bugs that they find.