passing multiline text from textarea (very easy)

Discussing Html . Code , Software , other

Moderators: macek, egami, gesf

sergio_mi
New php-forum User
New php-forum User
Posts: 23
Joined: Thu Apr 22, 2004 7:00 am

passing multiline text from textarea (very easy)

Postby sergio_mi » Tue May 11, 2004 1:02 am

passing multiline text from textarea to another page
I have as result a one-line-only text.
I know it's easy, but this morning my memory won't work.
thanks

User avatar
gesf
Moderator
Moderator
Posts: 1717
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Postby gesf » Tue May 11, 2004 1:42 am

It´s possible for you show us your code?

sergio_mi
New php-forum User
New php-forum User
Posts: 23
Joined: Thu Apr 22, 2004 7:00 am

more details

Postby sergio_mi » Tue May 11, 2004 5:34 am

I've a form with a textarea.
in a textarea you can obviously write some text on more lines like what i'm doing now writing this message.
but if I submit it to a new page (like when I preview this message) for instance in a cell table, the text is displayed only on one line without any "new line" \n;

the output is like: text on a line text on a new line

there no code because it's plain html

if you need more details please ask!
thanks.

User avatar
gesf
Moderator
Moderator
Posts: 1717
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Postby gesf » Tue May 11, 2004 6:14 am

Please check if your cell(td), or the place where the text should appear and even your textarea has a property called nowrap.
It indicates that the text should not wrap.

sergio_mi
New php-forum User
New php-forum User
Posts: 23
Joined: Thu Apr 22, 2004 7:00 am

more and more details

Postby sergio_mi » Tue May 11, 2004 7:22 am

<form action="b.php" method="get">
<textarea rows="10" cols="20" name="txt"></textarea>
<input type="submit" value="ssss">
</form>
I write:
line1
line2
line3
line4

in the url bar I see: b.php?txt=line1%0D%0Aline2%0D%0Aline3%0D%0Aline4

the receiving page:
<? $str = $_GET['txt']; ?>
<table><tr><td>
<? echo $str; ?>
</td></tr></table>

the output is:
line1 line2 line3 line4

even setting height value in td nothing changes

as you can see no wrap.

User avatar
gesf
Moderator
Moderator
Posts: 1717
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Postby gesf » Tue May 11, 2004 8:41 am

Use this code:

Code: Select all

<?php
$str = str_replace('\n', '<br>', $_GET['txt']);
?>
Instead of yours ...

Code: Select all

<?php
$str = $_GET['txt'];
?>

This should solve your problem ;)

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Tue May 11, 2004 1:01 pm

Better yet, just use nl2br().

The reason for this problem, sergio, is that when an HTML document is displayed in the browser, all whitespace (tabs, newlines, and multiple spaces) are converted to single spaces for display. This is intentional and the correct behavior of the browser. If you open your document in the brower and go to View > Source, you'll see that your newlines are still there; they're just not displayed. In order to do a line break in HTML, we have to use the <br/> tag, hence nl2br().

User avatar
gesf
Moderator
Moderator
Posts: 1717
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Postby gesf » Tue May 11, 2004 10:19 pm

Great... i forgot that function! Yeh it´s better :-D

sergio_mi
New php-forum User
New php-forum User
Posts: 23
Joined: Thu Apr 22, 2004 7:00 am

ok!

Postby sergio_mi » Wed May 12, 2004 7:40 am

grazie swirlee.
sei troppo avanti.
thanks to you too, gesf.


Return to “HTML Basics”

Who is online

Users browsing this forum: No registered users and 1 guest

cron