...

View Full Version : Resolved Span tag dilemma!



snarf1974
02-15-2009, 09:55 PM
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:

http://www.glentechsolutions.co.uk/images/form-error.jpg

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

http://www.glentechsolutions.co.uk/images/form.jpg

This is the span tag:


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


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

_Aerospace_Eng_
02-15-2009, 10:10 PM
I don't know your logic as you haven't showed us your php but do something like

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

Majoracle
02-15-2009, 10:14 PM
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:

$errors['name'] = !is_null($errors['name']) ? "<span class=\"error\">".$errors['name']."</span>" : null;and
<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.

Sorry, _Aerospace_Eng_. Use his, it's the same principle, but it's faster.

snarf1974
02-15-2009, 10:48 PM
Thank you very much _Aerospace_Eng_ and Majoracle, both solutions work great, but like you said Majoracle, _Aerospace_Eng_ solution is faster
:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum