Question

EDIT: After re-reading my post I think I am being a little bit unclear about what the problem is. Let me try to re-phrase it:

Users can leave comments on my site using a textarea field in a form. It should be possible to add line breaks in the comment using <br />. The comment is then stored as a string in a mysql-database (escaped to make it safe) and later on retrieved from the database and displayed on the site. Regular line breaks are not recognized when the comment is displayed.

Users can edit their comments, and in that case the original comment is displayed in a textarea field to be modified (see image below). The problem is that if there are any <br /> present, the code is not valid as XHTML 1.0 (see error message below).

Can I make the code valid using any other type of line break? As stated in the comments, regular line breaks in combination with xml:space="preserve" does not work (line breaks are displayed in the textarea field, but not when the comment is displayed as normal text on the site).


Original question:

I have a web-form which includes a textarea field to leave comments, and I want it to be able to add line breaks using <br />. Below is a simple example:

screen dump
(source: shipit.se)

It works as intended, however it does not validate as XHTML 1.0 transitional when using the W3C validation service. This is the error I get:

  1. Error Line 90, Column 1587: document type does not allow element "br" here

…ription" rows="0" cols="0">Test<br />line break</textarea></dd><dt class="cha

The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).

One common cause for this error is the use of XHTML syntax in HTML documents. Due to HTML's rules of implicitly closed elements, this error can create cascading effects. For instance, using XHTML's "self-closing" tags for "meta" and "link" in the "head" section of a HTML document may cause the parser to infer the end of the "head" section and the beginning of the "body" section (where "link" and "meta" are not allowed; hence the reported error).

Is there a way to make the line breaks valid code, or do I need to find a workaround (like e.g. using specific tags for line breaks in the textarea field and then replacing them with <br /> later on when displaying the comment)?

Was it helpful?

Solution

From the DTD:

<!ELEMENT TEXTAREA - - (#PCDATA) -- multi-line text field -->

In the XHTML code of your page, a <textarea> can only contain text (#PCDATA), and you cannot nest any XHTML elements within the <textarea>.

Your questions seems to show two different things. The image shows "<br />" written into a <textarea>, while the message from the W3C Validator is referring to a <br /> element written into the XHTML of your page within the <textarea> element.

In the first case, having "Test<br />line break" appear to the user in the text area is done by using the appropriate entities, just as altCognito wrote:

<textarea>Test&lt;br /&gt;line break</textarea>

Anything that's being entered by the user that is then redisplayed within a <textarea> on a new page should be encoded (i.e., use entities for &, ", ', <, and >).

If want to display a user's entry:

Test<br />line break

...as...

Test
line break

...within another text area, then you will need to parse what has been entered into the original <textarea> and replace the user-entered <br />s with normal line breaks. See Lucero's answer.

OTHER TIPS

Have you tried adding a xml:space="preserve" attribute and use just plain normal line breaks?

Why not use regular linebreaks? Those work just fine.

If you want <br /> to appear, you need to encode those entities:

&lt;br /&gt;

i just did this and works.

<textarea name="contenidoMensaje" id="contenidoMensaje" cols="80" rows="10">
-----Mensaje Original-----
{blank space}
<?php echo str_replace("<br />", "\n", $contenidoMensaje); ?>
</textarea>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top