Developer Support »

Handling multi-page forms

This applies to both scripts, FormToEmail and FormToEmail-Pro

In a conventional setup you would have one form page which is submitted to the script to be processed. You might require multiple form pages where a user enters values in one form then proceeds to another form and so on, with the final form page getting submitted to the script. To process multi-page forms you need to add some PHP code to your form pages and to the script. The code differs between the free script (FormToEmail) and the Pro script (FormToEmail-Pro) as the Pro script can also handle file inputs which are uploaded and sent as attachments (the files can be uploaded on any or all of the multi-page form pages).

Code for using with the free script (FormToEmail)

At the top of each of your form pages (except the first) you need to paste in this code:

session_start();

if(!$_SESSION['ses_request']){$_SESSION['ses_request'] = $_REQUEST;}else{$_SESSION['ses_request'] = array_merge($_SESSION['ses_request'],$_REQUEST);}

At the top of the script (FormToEmail.php) you need to paste in this code:

session_start();

if($_SESSION['ses_request']){$_REQUEST = array_merge($_SESSION['ses_request'],$_REQUEST);}

That's all (but see the notes at the bottom of this page). Save the form page files and the script and upload them to your webspace.

Code for using with the Pro script (FormToEmail-Pro)

At the top of each of your form pages (except the first) you need to paste in this code:

session_start();

if($_FILES)
{

$path_to_uploaded_file = dirname(__FILE__) . "/";

foreach(array_keys($_FILES) as $value){if(!empty($_FILES[$value]['tmp_name'])){move_uploaded_file($_FILES[$value]['tmp_name'], $path_to_uploaded_file.$_FILES[$value]['name']);}}

if(!$_SESSION['ses_files']){$_SESSION['ses_files'] = $_FILES;}else{$_SESSION['ses_files'] = array_merge($_SESSION['ses_files'],$_FILES);}

}

if(!$_SESSION['ses_request']){$_SESSION['ses_request'] = $_REQUEST;}else{$_SESSION['ses_request'] = array_merge($_SESSION['ses_request'],$_REQUEST);}

At the top of the script (formtoemailpro.php) you need to paste in this code:

session_start();

$path_to_uploaded_file = dirname(__FILE__) . "/";

if($_FILES)
{

foreach(array_keys($_FILES) as $value){if(!empty($_FILES[$value]['tmp_name'])){move_uploaded_file($_FILES[$value]['tmp_name'], $path_to_uploaded_file.$_FILES[$value]['name']);}}

if($_SESSION['ses_files']){$_FILES = array_merge($_SESSION['ses_files'],$_FILES);}

}elseif($_SESSION['ses_files']){$_FILES = $_SESSION['ses_files'];}

if($_SESSION['ses_request']){$_REQUEST = array_merge($_SESSION['ses_request'],$_REQUEST);}

Near the bottom of the script (formtoemailpro.php) locate this code:

if(is_uploaded_file($_FILES[$value]['tmp_name']))
{

$file = fopen($_FILES[$value]['tmp_name'],'rb');
$data = fread($file,filesize($_FILES[$value]['tmp_name']));
fclose($file);
$data = chunk_split(base64_encode($data));

$attachment_array[] = "--boundary_sdfsfsdfs345345sfsgs".PHP_EOL."Content-Type: ".$_FILES[$value]['type'].";".PHP_EOL." name=\"".$_FILES[$value]['name']."\"".PHP_EOL."Content-Disposition: attachment;".PHP_EOL." filename=\"".$_FILES[$value]['name']."\"".PHP_EOL."Content-Transfer-Encoding: base64".PHP_EOL.PHP_EOL.$data.PHP_EOL.PHP_EOL;

}

Replace it with this code

if(!empty($_FILES[$value]['name']))
{

if(file_exists($path_to_uploaded_file.$_FILES[$value]['name']))
{

$file = fopen($path_to_uploaded_file.$_FILES[$value]['name'],'rb');
$data = fread($file,filesize($path_to_uploaded_file.$_FILES[$value]['name']));
fclose($file);
$data = chunk_split(base64_encode($data));

$attachment_array[] = "--boundary_sdfsfsdfs345345sfsgs".PHP_EOL."Content-Type: ".$_FILES[$value]['type'].";".PHP_EOL." name=\"".$_FILES[$value]['name']."\"".PHP_EOL."Content-Disposition: attachment;".PHP_EOL." filename=\"".$_FILES[$value]['name']."\"".PHP_EOL."Content-Transfer-Encoding: base64".PHP_EOL.PHP_EOL.$data.PHP_EOL.PHP_EOL;

// Delete uploaded file

unlink($path_to_uploaded_file.$_FILES[$value]['name']);

}

}

That's all (but see the notes below). Save the form page files and the script and upload them to your webspace.

Notes for both scripts

No PHP code in the first form page. In this case, there is no PHP code required in the first page of a multiple series of form pages. The first page can be a straight HTML page. All of the subsequent form pages do require the code.

Form pages must be php. Because you are using PHP code, your form pages must be named with a .php extension, like this for example: contact2.php, contact3.php etc etc. If you don't have PHP code in your form page and the only code you are adding is the code above, then you must precede the code with "<?php" and end it with "?>" like so:

<?php

php code

?>

If there is already PHP code on your form page, then simply paste in the above code at the top of your existing code (should be ok in most cases). When pasting the above code into the script, you do not need to enter the opening and closing PHP tags above (they will already exist) just paste the code in the top of the script (and the body for the Pro version) inside the opening tag.

Forms point to the next. With multi-page forms, each form "points" to the next form in the sequence, with the LAST form pointing to the script. This means that you must set the form <action> accordingly on each form page. So on an example form page (contact2.php) the action would look like this:

<form action="contact3.php" method="post">

In this example we will say that contact3.php is the LAST form page so we must make the action point to the script, like so (free script in this example):

<form action="FormToEmail.php" method="post">

Session cookies. The above code uses a session to store the form values. This means you must have sessions enabled in your PHP setup. It also usually means (depends on your setup) that your visitors will need to have cookies enabled in their browser.

Any number of form pages. With the code above you can have any number of form pages. Just remember that you do not need to put any code in your first page (it won't do any harm there but it's meaningless) and that each form page points to the next, with the last one pointing to the script.

Array inputs. Both scripts handle array inputs such as a multiple select or checkboxes with the same name. This is just the same when processing multiple page forms, just make your forms as you would do normally, the scripts will handle them just the same.

Unique field names. All your form inputs/fields must have unique names. It doesn't matter if they are on different pages, all the information from the forms is gathered then submitted in one hit to the script, so all names must be unique.

Clearing the session. It might be advantageous (normally is) to clear the session data after the form has been successfully processed. This means if the user has to use your form again, the new data will be in a new session. This prevents old data getting submitted again. To clear the session, locate a part of the script that is actioned after the email is sent, after this line is ideal:

mail($my_email,$subject,$message,$headers);

Below the line, paste this code:

session_destroy();

That's it.

Notes for Pro script

With the Pro version of the script you can process file uploads and send them as attachments. This applies just the same when you have multiple page forms. You can have file inputs on any or all of your form pages from the first to the last. They are handled slightly differently for multi-page forms. The uploaded file from the visitor's computer is moved to a specified directory (folder) on your server (overwriting any of the same name that might reside there). The files are kept there until the final form page is submitted and the forms are processed. The files are attached to the email sent from the script, thereafter they are deleted.

If a form page has a file input you must put enctype="multipart/form-data" in the form action and use the "post" method, like so:

<form action="nextpage.php" method="post" enctype="multipart/form-data">

Script and forms in same directory (default). The code above assumes that the form pages, the script and the uploaded files are all in the same directory (folder). It is advisable to keep the uploaded files in a separate directory because as they are deleted after use there is the risk that an important file with the same name as an uploaded file could get deleted. To specify a directory for the uploaded files, locate every instance of this line on every form page that has the above code in it and in the script:

$path_to_uploaded_file = dirname(__FILE__) . "/";

Change it to the desired directory. Note the full path is used and note also the trailing slash:

$path_to_uploaded_file = "/home/user/uploads/";

Ensure that the directory permissions are correct to allow PHP to write to the directory. On Linux type systems CHMOD 770 is typically sufficient. 777 will work every time. See your host/admin for more information.

By default the above code will delete uploaded files when the forms are processed. If you wish to keep them, then delete this line of code (or change it to a comment):

unlink($path_to_uploaded_file.$_FILES[$value]['name']);

To change it into a comment, that gets ignored by PHP, precede it with two forward slashes, like this:

//unlink($path_to_uploaded_file.$_FILES[$value]['name']);

Check for required fields. The Pro version of the script can check for required fields. This works just the same when handling multi-page forms but the check is done at the end of the form-filling/submitting process when all of the accumulated data (saved in the session) is submitted to the script. This is the disadvantage of using multiple forms in this instance as the user would have to go all the way back to correct an error, then start again. You can get around this by using JavaScript to validate each form as it is submitted (assuming the user has JavaScript enabled) but this is beyond the scope of this support.