Reload PHP Form Data After Validation

Hi everyone! I’m hoping you can help -

I currently have a form - Hotline.php

The form contains multiple fields to include text, textarea, options, and checkboxes. There are three required fields. When a user tries to submit the form (submits to itself) and they are missing the fields, the error message displays at the top of the page.

What I would like to have happen after the validation and the displaying of the error message, is that the form reload the data that was initially part of the submission.

I’ve tried setting the values of the input fields to the PHP value. Example:
[php][/php]

This worked well for input text types, but I don’t know how to do the same with checkboxes, drop-down lists, or text areas.

If there is another, better and/or easier way to accomplish this, please let me know!

You will get better answers if you post all the code you are using.

In any case, you want to do “if !empty” on the values or you will get errors. (If error reporting is on)

[php]value="<?= !empty($_POST['field_name']) ? $_POST['field_name'] : '';?>"[/php]

Sure, this is the gist of the code I’m using. There’s quite a few form fields, so I just left one of each type being used:

PHP Portion of Hotline.php
[php]

<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { // Receiving variables //Checkbox field @$from = addslashes($_POST['from']); //Text @$submitName = addslashes($_POST['SubmitName']); //Text Area @$address = addslashes($_POST['Address']); //Select (dropdown) @$AllegedStatus = addslashes($_POST['AllegedStatus']); //Required Field @$AllegedLN = addslashes($_POST['AllegedLN']); //Required Field @$AllegedFN = addslashes($_POST['AllegedFN']); //Required Field @$AllegedQ7 = addslashes($_POST['AllegedQ7']); //Validation $required = array($AllegedFN, $AllegedLN, $AllegedQ7); $errors = array(); $isErrors = false; if ($required[0]=="") { $errors[0] = "Error Message 1"; $isErrors = true; } if ($required[1]=="") { $errors[1] = "Error Message 2"; $isErrors = true; } if ($required[2]=="") { $errors[2] = "Error Message 3"; $isErrors = true; } if ($isErrors== true) { $errorString = '

Please fill out the following fields:

'; $errorString .= '
    '; foreach($errors as $error) { $errorString .= "
  • $error
  • "; } $errorString .= '
'; } else { //Sending Email to form owner - This part works fine echo("

Thank you.

"); } } ?>[/php]

HTML portion of Hotline.php

[php]

<?=$errorString?>
<input type="checkbox" name="from" value="confidential">
    Check this box if you want to remain confidential.
    <p>
        Name:<br/>
        <input type="text" name="SubmitName" style="width:100%" value="<?=$submitName?>">
    </p>
    <p>
        Address:<br/>
        <textarea name="Address" id="address" rows="3" style="width:100%"></textarea>
    </p>

</div>
<p>
<input type="checkbox" name="from" value="anonymous">
	Check this box if you want to remain anonymous.  Your e-mail address will not be available.        
</p>

Person's Status: <br/>
	<select name="AllegedStatus" style="width:100%">
    	<option selected value="">&nbsp;</option>
    	<option value="USDA Employee">USDA Employee</option>
        <option value="General Public">General Public</option>
        <option value = "Contractor">Contractor</option>
        <option value = "Other">Other</option>
    </select>
    <p>
	Person's Last Name:<span style="color:red">*</span><br/>
	<input type="text" name="AllegedLN" style="width:100%" value="<?=$AllegedLN?>">
    </p>
    <p>
	Person's First Name:<span style="color:red">*</span><br/>
	<input type="text" name="AllegedFN" style="width:100%" value="<?=$AllegedFN?>">
</p>

<p>
	Question 7:<span style="color:red">*</span><br/>
    <textarea name="AllegedQ7" rows = "3" style="width:100%" value="<?=$AllegedQ7?>"></textarea>
</p>
 <input type="submit" name="Submit"  value="" style="background-image:url(images/submit_button.jpg); width:98px; height:23px; background-repeat:no-repeat; border:none">
[/php]

So, I ultimately want to be able to reload the values into those types of fields. I can do this successfully with text fields by setting the value to the PHP value, like this:
[php][/php]

First thing, get rid of ALL the @ symbols. You never want to hide errors. Errors are your friend. They tell you when something is wrong so you can fix it.

There is no need to number your error indexes. PHP will automatically do it for you

$errors[1] Can just be $errors[]

Your naming conventions are non- standard (AllegedLN) you have mixed case which is not camelCase. It can cause you problems on a larger application when you have one of the letter case wrong. Use one of the two standard naming conventions. Either camelCase or under_score. I_prefer_the_latter_as_I_feel_it_is_easier_to_read_on_long_names.

Kevin Rubio - thank you for those suggestions! I’ll work on standardizing the naming conventions.

Some further clarification on the error index.

Should it change from this:
[php]$required = array($AllegedFN, $AllegedLN, $AllegedQ7);
$errors = array();
$isErrors = false;
if ($required[0]=="")
{
$errors[0] = “Error Message 1”;
$isErrors = true;
}
if ($required[1]=="")
{
$errors[1] = “Error Message 2”;
$isErrors = true;
}
if ($required[2]=="")
{
$errors[2] = “Error Message 3”;
$isErrors = true;
}
[/php]

to this?

[php]$required = array($AllegedFN, $AllegedLN, $AllegedQ7);
$errors = array();
$isErrors = false;
if ($required[0]=="")
{
$errors[] = “Error Message 1”;
$isErrors = true;
}
if ($required[1]=="")
{
$errors[] = “Error Message 2”;
$isErrors = true;
}
if ($required[2]=="")
{
$errors[] = “Error Message 3”;
$isErrors = true;
}
[/php]

Each error message will just add to the array, not replace the previous one? So, assuming all three fields were not filled out, the array would have three indexes?

So, assuming all three fields were not filled out, the array would have three indexes?

Yes. Below is how I handle my form errors. (The error display has additional bootstrap code that you dont need.)

[php]<?php
$error = array();

if (empty($_POST[‘company_type_id’]))
{
$error[‘company_type_id’] = ‘Company Type Required.’;
}
if (empty($_POST[‘company_name’]))
{
$error[‘company_name’] = ‘Company Name Required.’;
}
if (empty($_POST[‘company_email’]))
{
$error[‘company_email’] = ‘Email Address Required.’;
}

if ($result = count($error) > 0)
    {
    show_form_errors($error);
    }

function show_form_errors($error)
{
$error = implode("
\n", $error) . “\n”;
echo <<<EOT



$error



EOT;
}
?>[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service