Log in

View Full Version : Resolved checking $error_msg against empty field, trying to check false



?foru
04-26-2009, 03:40 AM
I've been using an anti-spam contact form for a while now and I've adapted it to write to a database, send a page url...etc.

This time I'm using it just as a contact form, but want to highlight empty fields which I have partially working. (issue is not showing highlighted field when filled out and submit is hit again)

I found this http://kalsey.com/simplified/form_errors/ but it is written a little differently and sparked some ideas.

Start of the checking in my form...general statement about empty fields...

if (isset($c['submit'])) {
if (empty($c['recipient']) || empty($c['firstname']) || empty($c['lastname']) || empty($c['email']) || empty($c['contact_method'])|| empty($c['comments'])) {
$error_msg .= "All fields marked with an * are required. \n";

Then each field is check more in depth like so...

} elseif (strlen($c['firstname']) > 10) {
$error_msg .= "The name field is limited at 10 characters. Your first name will do. \n";
....email...other fields....etc.

Now, how this ties into the form field itself...

<input name="firstname" type="text" <?php if (!empty($error_msg['firstname'])) echo 'class="req"'; ?> id="firstname" value="<?php get_data("firstname"); ?>" size="30">

I tried to check against the field in 2 ways with && AND but I kept getting BOOLEAN errors. ex. if (!empty($error_msg['firstname'])) && something else

<?php if (!empty($error_msg['firstname'])) echo 'class="req"'; ?>
does it's job to echo class="req" which highlights the field, but my problem is returning a false if the field does get filled out to not show the field highlighted class like there is an error.

basically...
field empty - highlighted -- user goes back and fills it in and submits but has another error..the first error they fixed isn't highlighted anymore.

Does anyone have any ideas to change <?php if (!empty($error_msg['firstname'])) echo 'class="req"'; ?> to not show the class if the field is ok? Thank you!

?foru
04-26-2009, 10:02 PM
I came up with the following which highlights the field when the form is first shown...once that field is filled in and the form is submitted again it isn't highlighted anymore which is what I was almost going for.

<?php echo empty($error_msg['lastname']) ? 'class="req"' : ''; ?>

Since the field is highlighted from the start class="req" is being executed...after field is corrected '' is being executed since it clears and is no longer highlighted.

Anyway to re-write the above or add something after the ? mark so that...

the field isn't highlighted on start
if error highlight - class="req"
if error cleared in that field (and another error in some other field) don't highlight the corrected field anymore?

?foru
04-27-2009, 04:05 AM
This redundant mess doesn't throw any parse errors, but doesn't clear class="req" when a value is filled in.

<?php
if (empty($error_msg['lastname'])) {
echo "";
} elseif (!empty($error_msg['lastname'])) {
echo "class=\"req\"";
} elseif (!empty($c["lastname"])) {
echo "";
} else {
echo "";
}
?>
So the first two things are taken care of, just trying to figure out now how to clear class="req" when the value is filled in like value="Jones"

value is actually value="<?php get_data("lastname"); ?>" so I thought that I needed to check against that value as well.

get_data comes from this function...

function get_data($var) {
global $c;
if (isset($c[$var])) {
echo $c[$var];

I'm not sure where to go from here because I've tried everything that I can think of. Any help would be greatly appreciated.

?foru
04-27-2009, 06:37 PM
The following also works to not show the error when there isn't one, and it shows it when there is one.

$class = '';
if (!empty($error_msg['field1'])) {
$class = 'class="req"';
}
print "{$class}";

The issue I'm running into though is for instance if I have field1 and field2 and the user doesn't fill out either one, both show the error class like it should, but if say only field1 is filled out and the form is re-submitted it shows the value the user input into field1 but still shows the class like there is still an error.

<input name="field1" type="text" class="req" id="field1" value="ABC" size="30">

So that's why I was thinking if a value exists like value="ABC" check that as well and don't show the class when something is present ...my code above ...(!empty($c["lastname"]))... That way it doesn't confuse a user if they have properly filled it in and they still see the highlighted field indicating an error.

?foru
04-28-2009, 04:29 AM
Issue is resolved