Results 1 to 5 of 5

Thread: checking $error_msg against empty field, trying to check false

  1. #1
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default checking $error_msg against empty field, trying to check false

    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...
    PHP Code:
    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...
    PHP Code:
    } 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...
    PHP Code:
    <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!
    Last edited by ?foru; 04-28-2009 at 04:30 AM.

  2. #2
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    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.

    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?
    Last edited by ?foru; 04-26-2009 at 10:08 PM.

  3. #3
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    This redundant mess doesn't throw any parse errors, but doesn't clear class="req" when a value is filled in.
    PHP Code:
    <?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...
    PHP Code:
    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.

  4. #4
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    The following also works to not show the error when there isn't one, and it shows it when there is one.
    PHP Code:
    $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.

  5. #5
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Issue is resolved

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •