Prevent Error Message from Shifting Form

I have a simple Q&A Form laid out like this…


<label>
<textarea>
<error message>

How can I add a “place holder” for the Error Messages, so when they occur my Fields don’t shift down?!

Here is my HTML/PHP…


<form id="changeAnswers" action="" method="post">
	<fieldset>
		<legend>Change Profile Answers</legend>

		<?php
			// Display Q&A's.
			foreach($thoughtsArray as $questionNo => $qaArray){
				// Build Question.
				echo '<label for="question' . $questionNo . '">' . $questionNo . '.) ' . $qaArray['questionText'] . "</label>\
";

				// Build Answer.
				echo '<textarea id="question' . $questionNo . '" name=answer[' . $qaArray["questionID"] . ']" cols="60" rows="2">';
				echo (isset($questionNo) ? htmlentities($qaArray['answerText'], ENT_QUOTES) : '');
				echo "</textarea>\
\
";

				// Display Error.
				if (!empty($errors[$qaArray['questionID']])){
					echo '<br /><span class="error">' . $errors[$qaArray['questionID']] . "</span><br />\
\
";
				}
			}
		?>

		<!-- Submit Form -->
		<input type="submit" name="changeAnswers" class="button" value="Change Answers"/>
		<input type="reset" class="button" value="Cancel" />
	</fieldset>
</form>

Thanks,

Debbie

Always echo the error message - set it to ’ ’ at the start and overwrite that if there is an error message to display.

Something like this…


	// Display Error.
	if (!empty($errors[$qaArray['questionID']])){
		echo '<br /><span class="error">' . $errors[$qaArray['questionID']] . "</span>\
\
";
	}else{
		echo "<br /><span class=\\"error\\">&nbsp;</span>\
\
";
	}

Debbie

P.S. Is there a cleaner way to code my ELSE?

I suppose it depends as to whether your error messages are just one line or will run to many lines.

If its just one line then you could just have the empty elements in place all the time taking up space and then just fill them with the error message when needed (which I guess is what you are doing anyway with the revised code above).

If error messages run to more than one line then there’s not much you can do as you can’t account for a variable height.

Do you want this moved to PHP?

Yes, Error Messages will only ever be one line.

So it sounds like what felgall recommended should suffice, right?

Debbie

Not sure if I follow your question.

Here is the code I am using now which is slightly cleaner…


	// Display Error.
	if (!empty($errors[$qaArray['questionID']])){
		echo '<span class="error">' . $errors[$qaArray['questionID']] . "</span>\
\
";
	}else{
		echo '<span class="error">&nbsp;</span>' . "\
\
";
	}

Apparently it is okay to place   in between single quotes in PHP - which is not what you would expect?!

Debbie

You dont actually need an if/else here.
If your script doesnt detect an error while filling the $errors array, have it stick   in the array instead of an error message, then just echo whatever’s in there.

If you’re going to apply noticeable formatting to the error span (background color, for example), then you’ll have to do it a bit differently.

Hi DD

Yep echo ‘&nbsp’; will do it, or change your error span to a div and set its height to 1 line if you want tidy markup.