Forum Moderators: coopster

Message Too Old, No Replies

Add style to error message

Styling an error message with php form validator

         

benji

10:51 am on Mar 15, 2009 (gmt 0)

10+ Year Member



How would I add styling to the error message that is returned, after failed validation of my php script?

ie:

$error_message .= 'The Email Address you entered does not appear to be valid.<br />';

What I would like is to have simple styling, such as font-family, size etc.

Ideally, I'd have my company logo at top of page too!

Any help, much appreciated :)

rocknbil

4:57 pm on Mar 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd do this via CSS. And I do it with lists too, not an (ugh) break tag. :-)

Say your validation checks for several things:


$errorList = '';
$requireds = Array(
'fname' => 'first name',
'lname' => 'last name',
'email'=> 'email address'
);
foreach ($requireds as $key=>$value) {
if ((! isset($_POST[$key])) or ($_POST[$key] == '')) {
$errorList .= "<li>The $value field is required</li>\n";
}

Of course, do not directly use $_POST variables . . .

Then you go on and check for a valid email pattern, and any other validations, concatenating to $errorList until you get all errors at one time.

Then,

if ($errorList != '') {
print "<p class=\"warning\">Your form contains the following errors:</p>\n";
print "<ul>$errorList</ul>\n";
}

The CSS:
.warning { font-weight:700; color#ff0000; }

This gives you a neat bulleted list of all errors. Style the UL if you like too.

You could even create program variables representing the classes as modifiable settings:

$warning_style="warning";

print "<p class=\"$warning_style\">Your form contains the following errors:</p>\n";
print "<ul class=\"$warning_style\">$errorList</ul>\n";