Support »

Show errors on the form page

Applies to Pro Plan users only

Should a visitor submit a form containing an error (such as an empty required field) there are three ways to display the error (or errors). By default, the error will be shown on a blank page on our site. You can customize this page with your own HTML or you can redirect back to your form and display the errors on your form page. This support item deals with the last of these options.

To use this option, your form page needs to be a "dynamic" page, like .php or .asp (PHP instructions are shown below). You need to have PHP on your server. You need to have PHP code on your form page to display the errors. You can add the PHP code to your existing page or you can get sample form code in the User Panel for a form that is already coded to show the errors.

Whether using your own form or the sample code, you need to enable the option on the Form Configuration page (of the desired form) in the User Panel. The option you need to enable is called "Show Errors on Form Page". You will also need to enter the form page URL in the field titled "Form Page URL".

If you are using your own form, you will need to add the following code to it at a point in the page where you want the errors to be displayed, BEFORE your form is displayed (if you are new to working with PHP code, have a look at the Editing PHP Code item in the Support section):

<?php

if(isset($_POST['formtoemailremote_errors'])) 
{

foreach(array_keys($_POST) as $key){if(!is_array($_POST[$key])){$_POST[$key] = stripslashes(urldecode($_POST[$key]));}else{foreach(array_keys($_POST[$key]) as $key2){$_POST[$key][$key2] = stripslashes(urldecode($_POST[$key][$key2]));}}}

print "<p style=\"color:#f00\">";

foreach($_POST['formtoemailremote_errors'] as $form_error_value){print "<strong>" . $form_error_value . "</strong><br>";}

print "</p>";

}

?>

The above code will display any errors in a bold red font, one line for each error.

Pre-populate the form (optional)

If you don't want your visitors to be brought back to a blank form, you can code your form to show the submitted values. In other words, you can pre-populate it. To do so, you need to put some PHP code into your HTML form code. For each form input on your form (where you expect a visitor to enter data, not a hidden field for example) you need to have a piece of code like this:

<?php if(isset($_POST['field_name'])){print $_POST['field_name'];}else{print "";} ?>

The above code is used to print a value in the form field. 'field_name' is replaced with the EXACT name of the form field.

Text inputs

Here's an example. Suppose you have an input on your form, like this:

<input type="text" name="Street_Address">

To show the visitor's entered value, put the above PHP code into a value for the field, like so:

<input type="text" name="Street_Address" value="<?php if(isset($_POST['Street_Address'])){print $_POST['Street_Address'];}else{print "";} ?>">

Note that "$_POST['Street_Address']" is used (two instances) in place of the generic "$_POST['field_name']". Do the same for all text inputs, replacing $_POST['field_name'] with the exact (case sensitive) name of the form field.

If you have default text that appears in a text input, include it in the code, like so:

<input type="text" name="Street_Address" value="<?php if(isset($_POST['Street_Address'])){print $_POST['Street_Address'];}else{print "Please enter your street address";} ?>">

"Please enter your street address" is the default text and is entered between the quotes.

Select inputs (drop-down lists)

For <select> inputs (drop-down lists) on your form, the coding is like this:

Change this:

<select name="fruit">
<option>Banana</option>
<option>Apple</option>
<option>Orange</option>
</select>

...to this:

<select name="fruit"> 
<option <?php if(isset($_POST['fruit']) && $_POST['fruit'] == "Banana"){print "selected=\"selected\"";} ?>>Banana</option> 
<option <?php if(isset($_POST['fruit']) && $_POST['fruit'] == "Apple"){print "selected=\"selected\"";} ?>>Apple</option> 
<option <?php if(isset($_POST['fruit']) && $_POST['fruit'] == "Orange"){print "selected=\"selected\"";} ?>>Orange</option> 
</select>

If you have a very long list of options, this cheat might work for you:

<select name="fruit"> 
<option><?php if(isset($_POST['fruit'])){print $_POST['fruit'];}else{print "";} ?></option> 
<option>Banana</option> 
<option>Apple</option> 
<option>Orange</option> 
</select>

To show a default selected option, use this additional piece of code:

if(!isset($_POST['fruit'])){print "selected=\"selected\"";}

Using the above code, if you want "Orange" to be the default selected option, use this code:

<select name="fruit"> 
<option <?php if(isset($_POST['fruit']) && $_POST['fruit'] == "Banana"){print "selected=\"selected\"";} ?>>Banana</option> 
<option <?php if(isset($_POST['fruit']) && $_POST['fruit'] == "Apple"){print "selected=\"selected\"";} ?>>Apple</option> 
<option <?php if(!isset($_POST['fruit'])){print "selected=\"selected\"";} if(isset($_POST['fruit']) && $_POST['fruit'] == "Orange"){print "selected=\"selected\"";} ?>>Orange</option> 
</select>

Textareas

For textareas, the coding is like this:

Change this:

<textarea name="comments" rows="6" cols="30"></textarea>

...to this:

<textarea name="comments" rows="6" cols="30"><?php if(isset($_POST['comments'])){print $_POST['comments'];} ?></textarea>

To show default text in a textarea, which is shown when the form is first displayed then overwritten by any user input, use this code:

<textarea name="comments" rows="6" cols="30"><?php if(isset($_POST['comments'])){print $_POST['comments'];}else{print "some default text";} ?></textarea>

Radio buttons

For radio buttons, the coding is slightly different, like this:

Change this:

<input type="radio" name="veg" value="cabbage"> cabbage
<input type="radio" name="veg" value="onion"> onion

...to this:

<input type="radio" name="veg" value="cabbage" <?php if(isset($_POST['veg']) && $_POST['veg'] == "cabbage"){print " checked=\"checked\"";} ?>> cabbage
<input type="radio" name="veg" value="onion" <?php if(isset($_POST['veg']) && $_POST['veg'] == "onion"){print " checked=\"checked\"";} ?>> onion

If you have set a default radio button with checked="checked" you need to add additional PHP code to that input. This means when the visitor first visits your form, they will get the default radio button checked. If they then select a different option and are returned to the form after an error, the new selected option will be shown as checked.

This is the additional piece of code:

if(!isset($_POST['veg'])){print " checked=\"checked\"";}

Using the above code example, if you want the "cabbage" option to be shown checked by default, the code would look like this:

<input type="radio" name="veg" value="cabbage" <?php if(!isset($_POST['veg'])){print " checked=\"checked\"";} if(isset($_POST['veg']) && $_POST['veg'] == "cabbage"){print " checked=\"checked\"";} ?>> cabbage
<input type="radio" name="veg" value="onion" <?php if(isset($_POST['veg']) && $_POST['veg'] == "onion"){print " checked=\"checked\"";} ?>> onion

Checkboxes

For checkboxes, again the coding is slightly different. With checkboxes there are two naming conventions: 1. Multiple checkboxes with the same name. 2. Single checkboxes or multiple checkboxes with different names. The code to prepopulate them differs slightly. Multiple checkboxes with the same name can be identified by square brackets after the name (this makes the input an array).

For multiple checkboxes with the same name, change this:

<input type="checkbox" name="fruit[]" value="apple">Apple
<input type="checkbox" name="fruit[]" value="orange">Orange
<input type="checkbox" name="fruit[]" value="banana">Banana

...to this:

<input type="checkbox" name="fruit[]" value="apple" <?php if(isset($_POST['fruit']) && in_array("apple",$_POST['fruit'])){print " checked=\"checked\"";} ?>>Apple
<input type="checkbox" name="fruit[]" value="orange" <?php if(isset($_POST['fruit']) && in_array("orange",$_POST['fruit'])){print " checked=\"checked\"";} ?>>Orange
<input type="checkbox" name="fruit[]" value="banana" <?php if(isset($_POST['fruit']) && in_array("banana",$_POST['fruit'])){print " checked=\"checked\"";} ?>>Banana

For single checkboxes or multiple checkboxes with different names, change this:

<input type="checkbox" name="fruit" value="apple">Apple

...to this:

<input type="checkbox" name="fruit" value="apple" <?php if(isset($_POST['fruit'])){print " checked=\"checked\"";} ?>>Apple

If you have set default checkboxes with checked="checked" you need to add additional PHP code to those checkboxes. This means when the visitor first visits your form, they will get the default checkboxes checked. If they then select different checkboxes or deselect a default checkbox and are returned to the form after an error, the new selections will be shown as checked.

This is the additional piece of code (same as for radio buttons):

if(!isset($_POST['fruit'])){print " checked=\"checked\"";}

Using the above code example for multiple checkboxes with the same name, if you want the "apple" checkbox to be shown checked by default, the code would look like this:

<input type="checkbox" name="fruit[]" value="apple" <?php if(!isset($_POST['fruit'])){print " checked=\"checked\"";} if(isset($_POST['fruit']) && in_array("apple",$_POST['fruit'])){print " checked=\"checked\"";} ?>>Apple
<input type="checkbox" name="fruit[]" value="orange" <?php if(isset($_POST['fruit']) && in_array("orange",$_POST['fruit'])){print " checked=\"checked\"";} ?>>Orange
<input type="checkbox" name="fruit[]" value="banana" <?php if(isset($_POST['fruit']) && in_array("banana",$_POST['fruit'])){print " checked=\"checked\"";} ?>>Banana

File inputs

You cannot show file names by using a value in a file input field. In other words, you can't pre-populate a file input field. For security reasons, browsers do not display such values. Any file input fields will be blank, and the user will have to select a file to upload once again.