Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2008
    Location
    Liverpool
    Posts
    53
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Question Span tag dilemma!

    Hi guys,

    I'm building a form, but having a problem with some span tags.

    If the form fields remain empty after the user has clicked submit, this error kicks in via PHP, using CSS to style the span tag:



    All well and good here, BUT, if the user navigates to this page (Contact Us), the span tag shows a border:



    This is the span tag:

    PHP Code:
    <input type="text" name="name" id="name" value="<?= htmlentities($values['name']) ?>" size="22" tabindex="1" class="required" /><span class="error"><?= $errors['name'?></span></p>

    And this is the CSS:

    Code:
    span.error {
    float: left;
    width: 305px;
    background: #FF0000;
    color: #fff;
    border: 1px solid #999;
    margin:4px 0 4px 0;
    padding-left: 5px;
    display: inline;
    }
    Is there a way to hide the border from the span tag, and only show it when the PHP validation kicks in? or, is there just a different/better way to achieve this?

    Many thanks
    Last edited by snarf1974; 02-15-2009 at 10:51 PM. Reason: Resolved

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I don't know your logic as you haven't showed us your php but do something like
    PHP Code:
    <span class="error" style="display:<?php echo ($errors['name'] != '') ? 'block' 'none'?>"><?= $errors['name'?></span>
    This will show the spans only if there are errors. Of course you'll need to adjust the logic a bit but you should get the idea.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    snarf1974 (02-15-2009)

  • #3
    Regular Coder
    Join Date
    Nov 2006
    Posts
    247
    Thanks
    13
    Thanked 26 Times in 24 Posts
    Well, as long as the span tag is there, the border will show. I see 2 options.

    1.) Get rid of the border

    2.) Carry the span tag over to your PHP. I assume when there's no error, you have $errors['name'] as null. just have something like:
    PHP Code:
    $errors['name'] = !is_null($errors['name']) ? "<span class=\"error\">".$errors['name']."</span>" null
    and
    PHP Code:
    <input type="text" name="name" id="name" value="<?= htmlentities($values['name']) ?>" size="22" tabindex="1" class="required" /><?= $errors['name'?>
    Then the span tag will only show when there's an error as opposed to all the time.

    Edit: Sorry, _Aerospace_Eng_. Use his, it's the same principle, but it's faster.
    Last edited by Majoracle; 02-15-2009 at 10:18 PM.

  • Users who have thanked Majoracle for this post:

    snarf1974 (02-15-2009)

  • #4
    New Coder
    Join Date
    Aug 2008
    Location
    Liverpool
    Posts
    53
    Thanks
    37
    Thanked 0 Times in 0 Posts
    Thank you very much _Aerospace_Eng_ and Majoracle, both solutions work great, but like you said Majoracle, _Aerospace_Eng_ solution is faster


  •  

    Posting Permissions

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