468,120 Members | 1,959 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,120 developers. It's quick & easy.

<textarea> input help

First of all, my apologies if this should be in a php newsgroup and
not here, but I thought this the best place to start.

I want to parse the text from a <textareaform element but when I
pass this in a POST request to the server and then output it using
PHP, all formatting is gone.

I was under the impresssion that the textbox would register any
carriage returns or line breaks and send them in the final text for
processing?

Is there any way of forcing the textarea input to send formatting data
as well?

Essentially the idea would be to turn a text box with this in:
"Hello <= carriage return
<= line break
World <= carriage return
<= line break
Foo <= carriage return
<= line break
Bar"

into
"<p>Hello</p>
<br />
<p>World</p>
<br />
<p>Foo</p>
<br />
<p>Bar</p>"

but i can't do this is the received string at the server is does not
contain any reference to those line breaks of carriage returns. I
can't HTMLify it if you will.

Many thanks for any help you can offer
Nov 20 '07 #1
5 2774
Icarus - iD_Ten_T helper wrote:
before I out the string to the page before hand I take the POST data
and put it in a variable. then i use the PHP function str_split() to
put it into an array and I don't see any html codes in the array
entries where a carriage return should be.
No messy arrays needed. The following seems to work ok, if you don't
mind extra <br>'s in the middle of a paragraph:

$value = str_replace(Chr(10), "<br>", $value);
echo "<p>$value</p>";

...though I usually display entered content in a table, two columns, with
the field at the left, and the visitor's input in the right. So the
above would be:

echo "<tr><td>Comment:</td><td>$value</td></tr>";

--
-bts
-Motorcycles defy gravity; cars just suck
Nov 20 '07 #2
In article <c3**********************************@e1g2000hsh.g ooglegroups.com>,
Icarus - iD_Ten_T helper <ig**************@gmail.comwrote:
>
I want to parse the text from a <textareaform element but when I
pass this in a POST request to the server and then output it using
PHP, all formatting is gone.

I was under the impresssion that the textbox would register any
carriage returns or line breaks and send them in the final text for
processing?
It does... The POSTed string is exactly what you see in the box, with
newlines and spaces and so on. The corresponding PHP variable (or array
element I guess) is a copy of this. As Yucca says, you're only losing
those when your beowser gets the string back and ignores them.

There is a standard PHP function 'nl2br' that you can apply to the string
before outputting it that should do exactly what you want. (You also
might want to look at 'htmlspecialchars()'.) See the String Functions
section of the manual.

If you wanted to preserve spacing as well, I think you'd have to write
your own function to convert spaces to '$nbsp;'s or something.

Cheers,
-- Pete --

--
================================================== ==========================
The address in the header is a Spam Bucket -- don't bother replying to it...
(If you do need to email, replace the account name with my true name.)
Nov 20 '07 #3
Pete:
$nbsp;
Twice in as many days. Have I missed something?

--
Jock
Nov 20 '07 #4
In article <b9**********************************@f3g2000hsg.g ooglegroups.com>,
John Dunlop <jo**@dunlop.namewrote:
>Pete:
>$nbsp;

Twice in as many days. Have I missed something?
AARGhhhh! Well, thanks for keeping check on me, anyway... (:-/)

-- Pete --

--
================================================== ==========================
The address in the header is a Spam Bucket -- don't bother replying to it...
(If you do need to email, replace the account name with my true name.)
Nov 20 '07 #5
On Tue, 20 Nov 2007 11:24:39 +0100, Icarus - iD_Ten_T helper
<ig**************@gmail.comwrote:
I want to parse the text from a <textareaform element but when I
pass this in a POST request to the server and then output it using
PHP, all formatting is gone.

I was under the impresssion that the textbox would register any
carriage returns or line breaks and send them in the final text for
processing?
Yes, it does. HTTP POST requests for textareas with existing user
agents, are done in plain text with carriage returns preserved.

Most probably you simply passed this data to the HTML output of your
PHP script without modifying it. Consequently, the CRLF appears in the
HTML source but HTML, in most places, isn't sensitive to multiple
whitespaces.
If you need to output pre-formatted data (which is exactly what you
request), then, use the PRE HTML element.

<pre>
**************************
** Pre-formatted text **
** can be put here **
**************************
</pre>
Is there any way of forcing the textarea input to send formatting data
as well?
It does.
Essentially the idea would be to turn a text box with this in:
"Hello <= carriage return
<= line break
World <= carriage return
<= line break
Foo <= carriage return
<= line break
Bar"

into
"<p>Hello</p>
<br />
<p>World</p>
<br />
<p>Foo</p>
<br />
<p>Bar</p>"
Awful non-sense non-semantic code.
The pre-formatted text I've shown above doesn't consist of paragraphs.
Moroever, you would have issues with multiple spaces.
Nov 21 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.