Developer Support »

Show errors on the form page

Applies to the Pro version only

By default, the script will issue any error messages on the screen after the form is submitted. The form is not shown, and the user has to hit "back" to get back to the form (the standard error message page can be customized, have a look at the "Customizing error messages" page on the support section for more information).

If you would like the user to be returned to your form page when errors occur and have the error messages shown along with the form, you will need to add some code to your form page to display the errors (the script can't do that for you). You will also need to enable the $show_errors_on_form_page option, and enter a value for the $form_page_url variable (instructions in the script). Your form page needs to be a PHP page, with a .php extension, like contact.php for example. If it's an HTML page, just save it with a .php extension. By displaying the errors on the form page, it will appear to the visitor that they have not left the page.

(If you are new to editing PHP code, have a look at the "Editing PHP code" page in the support section.)

Add this code to your form page:

Put this at the top (if "session_start();" is not already there):

<?php

session_start();

?>

Then put this code on your page where you would like the errors to appear:

<?php

if(isset($_SESSION['formtoemail_form_errors']))
{

print "<div style=\"color:#ff0000\">";

foreach($_SESSION['formtoemail_form_errors'] as $form_error_value){print "<b>" . stripslashes($form_error_value) . "</b><br>";}

print "</div>";

unset($_SESSION['formtoemail_form_errors']);

}

?>

It is recommended that you enable the $pre_populate_form option in the script and add the appropriate code for it in your form page (see the "Pre-populate the form" page in the support section). This means the form values will be retained in the form in the event of an error. Otherwise, a blank form will be shown.

Sample form code

Below is code for a basic contact form with name, email and comments fields. It is already coded to display the errors on the same page as the form and to show the submitted form values if a user has to return to it. You can use the code exactly as it is, all you need to do is enable the $show_errors_on_form_page option, enter a URL in the $form_page_url variable and enable the $pre_populate_form option in the script (instructions in the script). Equally, you can look at the sample code to give you an idea of how you should code your own form (the lines in the code might display wrapped in your browser, you can view the code here in a text file).

<?php

session_start();

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Contact Form</title>
</head>

<body>

<?php

if(isset($_SESSION['formtoemail_form_errors']))
{

print "<div style=\"color:#ff0000\">";

foreach($_SESSION['formtoemail_form_errors'] as $form_error_value){print "<b>" . stripslashes($form_error_value) . "</b><br>";}

print "</div>";

unset($_SESSION['formtoemail_form_errors']);

}

if(isset($_SESSION['submitted_form_values'])){extract($_SESSION['submitted_form_values']);}

?>

<form action="formtoemailpro.php" method="post">
<table border="0" style="background:#ececec" cellspacing="5">
<tr><td>Name</td><td><input type="text" size="30" name="name" value="<?php if(isset($name)){print stripslashes($name);}else{print "";} ?>"></td></tr>
<tr><td>Email</td><td><input type="text" size="30" name="email" value="<?php if(isset($email)){print stripslashes($email);}else{print "";} ?>"></td></tr>
<tr><td valign="top">Comments</td><td><textarea name="comments" rows="6" cols="30"><?php if(isset($comments)){print stripslashes($comments);} ?></textarea></td></tr>
<tr><td> </td><td><input type="submit" value="Send"></td></tr>
</table>
</form>

</body>
</html>

<?php

if(isset($_SESSION['submitted_form_values'])){unset($_SESSION['submitted_form_values']);}

?>