PDA

View Full Version : How to Change the Background of a Missing Field?


John_Saunders
09-11-2002, 11:16 PM
Can somebody tell me how I can change the background color of a form field when a user tries so submit a form and enters an incorrect value or leaves it blank?

I just need to know how to change the style of the incorrect fields. I would think it would be something like this:

<input name="name" type="text" size="25" value="<?php if (isset($_POST['name'])) { echo htmlChars($_POST['name']); } ?>" <?php if (isset($error)) { echo "class=\"highlight\""; } ?>>

Not only can I not get the class to show up when an error is made but I'm not sure how to get it to show up in the correct form field of the error the visitor made.

Here's the code that pertains to the error checking in my script. It currently just has a section in my page where a list of errors are displayed.


<?php

// Require name
if (empty($_POST['name'])) {
$error .= '<br><span class="error">You must enter your name.</span>';
}

// Require a valid e-mail address
if (!isEmail(stripData($_POST['email']))) {
$error .= '<br><span class="error">You must enter a valid e-mail address.</span>';
}

// Require a value for the inquiry field
if (empty($_POST['inquiry'])) {
$error .= '<br><span class="error">You must enter a value for the comment/question field.</span>';
}

?>


section in page that shows error:

<?php if (isset($error)) { echo "<span class=\"error\">The following errors were found:</span><br>"; } ?>
<?php if (isset($error)) { echo $error; } ?>


Any ideas? Would something like this work?


<?php
// Require name
if (empty($_POST['name'])) {
$error .= '<br><span class="error">You must enter your name.</span>';
}
else if () {
$error2 .='class="highlight"';
}
?>

Then in down in my form in the input tag add something like:

<?php if (isset($error)) { echo $error2; } ?>


John

php_brian
09-12-2002, 12:26 AM
Check out http://www.peterbailey.net/fVaildate. It's client-side, but gets the job done.

John_Saunders
09-12-2002, 12:30 AM
Hi Brian,

I've seen fValidate before, but I am trying to keep my validation server-side. I have all my error checking rules running OK, I would just like to change the background color of the incorrect form fields to red so the visitor can easily see what they need to change, because there is a lot of content to fill out in the registration form.

Any advice on how to do this would be greatly appreciated.


Thanks,

John

firepages
09-12-2002, 01:55 AM
well this is how I usually go about it...
$required is an array of required fields (I have seperate arrays for emails & passwords which require a different kind of authentication ...but sticking with these for now)...


<?
$required=array('uname','name','fname','gname','faname','age','nationality','job','email');

foreach($required as $r){
if(!$_POST[$r]){
${$r."_err"}='bgcolor="red"';
$err=1;
}
}

if(!$err){/*process*/}else{/*form is displayed again*/}
?>


now for each of the fields that did not validate the variable $fieldname_err; is created ...eg $uname_err

then in my form...


<?
.....
<td <?=$uname_err;?>><input type="text" name="uname" value=<?=$POST['uname'];?></td>
etc ....
?>


thats the basics of how I do it anyway.