Clean error message text for a humungous form

php newbie

I have a form which has a shed load of fields that are compulsory, but can’t figure out a neat way of coding the error message…

"These fields are blank

field1
field7
field 11 etc"

I can do it for each individual field but don’t want to repeat the same error message for each field, and can’t figure out where the loops/if {} should go.

Any help appreciated - my current messy code is

if ($forename == “” | $surname == “” | $address == “” | $landline == “” | $email == “” | $ni == “” | $licence == “” |
$car == “” | $dob == “” | $age == “” | $marital == “” | $height == “” | $weight == “” | $eyes == “” | $badge == “” |
$bnumber == “” | $bexpiry == “” | $sortcode == “” | $account == “” | $bankname == “” | $holder == “” | $emergencyname == “” |
$emergencyphone == “” | $relation == “” | $offences == “” | $ref1name == “” | $ref1address == “” | $ref1phone == “” |
$ref2name == “” | $ref2address == “” | $ref2phone == “” | $bankrupt == “” | $judgements == “” | $school1name == “” |
$school1address == “” | $school1dates == “” | $school1contact == “” | $illness == “” | $vision == “” | $glasses == “” |
$hearing == “” | $smell == “” | $signature == “” | $sigdate == “” | $refs == “” | $refsdate == “”) {
echo “

All required questions have not been answered.

Please click back on your browser to check your application and complete the missing information
before re-submitting.

”;
}

Hi there,

Not tested this method, but could this be of use (small example given):
[php]$required = array(“forename”,“surname”,“address”);
$errors = array();
if(isset($_POST[‘submit’])
{
foreach($required as $fieldname)
{
if(!isset($_POST[$fieldname]) || (isset($_POST[$fieldname]) && trim($_POST[$fieldname]) == “”)) //Check required field is set and has a value
{
$errors[] = $fieldname; //Put name of required field into array (just in case you want to specify the fields that errored)
}
}
if(!empty($errors)) //If fields were added to the array, something went wrong
{
echo “<p class=“warning”>All required fields were not filled in. Please go back and try again.

”;
}
else
{
“whatever else you do”
}
}
[/php]

Let me know if this has helped, or just confused you!

Sponsor our Newsletter | Privacy Policy | Terms of Service