Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Leading line break in textarea 1

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR

There must be a dumb solution but I'm too tired to find out by myself :

I have this data in the db :

Code:
English
French
German

[ ! ] Note the leading line break above "English"

Now, when I display the data in a textarea, the leading line break disappears :(

I know the leading line break is still alive because a nl2br on the data shows this :

Code:
<br />
English<br />
French<br />
German<br />

So, how do I display this leading linebreak in the textarea so that I can store it again in the database?

Thanks a lot! :)

 
can you provide a link to the page where this is not working? it sounds like a browser issue rather than anything to do with php.
 
Hello jpadie :)

Sorry, I can't post any link here. Too bad there is no private messaging available on this site.

Anyway, I've checked on multiple browsers and it's the same on all of them :

The leading line break is there in the textarea when I look at the HTML source but it doesn't show itself on the page.

HTML source :

Code:
<textarea name="form_elem_values[select][13]" class="field1">
English
French
German
Italian
Spanish
Portuguese
Romanian
Hungarian
Russian
Other</textarea>

PHP source :

Code:
<textarea name=\"form_elem_values[select][" . $j . "]\" class=\"field1\">" . $row[1]["elem_values"] . "</textarea><br />
 
your code shows that there is a line break present. otherwise English would be on the same line as the textarea tag.

so hopefully problem solved?
 
I wish it would! :)

The problem is that I need to have the leading line break visible not only in the source code but in the page as well.

The reason is that this textarea is a field meant for the construction a select menu. Each line of text represents an <option></option>. So, the first line being empty means that the select menu will have a blank default value displayed.
The user must see that a line break exists.
 
try this. ugly as sin though
Code:
$ta = trim($row[1]["elem_values"]);
$ta = "\n" . $ta;
echo <<<HTML
<textarea name="form_elem_values[select][$j]" class="field1">
{$ta}
</textarea>
<br />
HTML;

note that you need to use heredoc bit to make this work. if you would rather keep as it were then add two \n

then, i would recommend, trim the input before inserting into database and add back the number of blank lines that you want. otherwise i suspect that the blank lines may multiply.
 
Thanks but you're right about the ugliness ahaha
And it's not practical anyway.
But why exactly doesn't the leading line break show in the textarea?? I really don't get it.
 
i suspect it's a browser thing. it is implicitly trimming the leading line break because it doesn't believe that anyone would want it (and there is no text). i suspect if you put an &amp; before the break, the browser would honour it anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top