Forum Moderators: coopster

Message Too Old, No Replies

check empty (short snippet I created) only partially working

         

php4U

12:55 am on Apr 27, 2009 (gmt 0)

10+ Year Member



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 Code:

<?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.

If I understand it right the above is saying EMPTY show class="req" if not nothing shows...which makes since because by default it is empty so class="req" is shown hence the field is highlighted.

Is there 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?

3 pieces instead of 2?
I appreciate any help on this, I've tried just about all I know to try to get it to work.

[edited by: php4U at 12:57 am (utc) on April 27, 2009]

eeek

1:35 am on Apr 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



3 pieces instead of 2?

So just break it down into an if-else-if structure.

php4U

2:22 am on Apr 27, 2009 (gmt 0)

10+ Year Member



Thanks for the reply. I didn't know if there was easier way to it other than if - else - if

I am having issues with the following because it isn't the usual if ($var == 'x')

unexpected { in line 207 which is the middle line.

<?php
if (empty($error_msg['lastname'])) { echo ''; }
else (!empty($error_msg['lastname'])) { echo 'class="req"'; }
if (empty($error_msg['lastname'])) { echo ''; }
?>

I've been working with this since your post and can't see where my error is.

php4U

3:21 am on Apr 27, 2009 (gmt 0)

10+ Year Member



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 "";
}
?>

<?php get_data("lastname"); ?>
is how the data is shown after it posts so the user doesn't have to fill it in again.

get_data comes from this function...

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

so I thought that maybe I need to check against the form value to see if something is there (!empty($c["lastname"]))

Anyone have any thoughts, or suggestions? Thanks again.

eeek

4:16 am on Apr 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I didn't know if there was easier way to it other than if - else - if

Well, you'd have been done if you had done it that way.

php4U

4:27 am on Apr 27, 2009 (gmt 0)

10+ Year Member



I posted just to get your wonderful rhetoric.

coopster

1:44 pm on Apr 27, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm having a difficult time following the logic. If you are using a single class to set style when there are error messages on fields in a form you can do so by initializing the field to blank and setting a value upon error. A simplified example:
$class = ''; 
if (error in form field1) {
$class = ' class="req"';
}
print "<input{$class} name="\field1\">";
$class = '';
if (error in form field2) {
$class = ' class="req"';
}
print "<input{$class} name="\field2\">";

php4U

2:53 pm on Apr 27, 2009 (gmt 0)

10+ Year Member



coopster,

the following works to not show the error when there isn't one, and it shows it when there is one.

$class = '';
if (!empty($error_msg['lastname'])) {
$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 I started to think if a value exists like value="ABC" check that as well and don't show the class when something is present. 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.

coopster

11:17 pm on Apr 27, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yes, that is exactly what you need to do.

php4U

12:35 am on Apr 28, 2009 (gmt 0)

10+ Year Member



coopster,

I really appreciate your posts. I was on the right track the whole time in thinking that, but that is what I was having difficulty with (everything I tried).

I tried something like this in post 4, but got the same result.
//field isn't empty
elseif (!empty($c["field1"])) {echo "";}

I took another look at the code in your last post, and created a working solution with the following...

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

Thanks again