David Laberge
David Laberge

Reputation: 16051

How to POST back a textarea with enters?

I have a HTML / Javascript page with some textarea. The user are typing their text with enter to create new lines. The problem lies when the form is submitted via ajax (with the functionnality of jQuery) to PHP. The text is received but without the enter.

How do you pass enters from HTML textarea to PHP?

I'm in POST in this context.

The textarea content the following

asdf

éè ï

"

'

``

||

@£¢¤¬¦²³³

But the PHP receive the following:

asdféè ï"'``||@£¢¤¬¦²³³

Upvotes: 0

Views: 914

Answers (3)

David Laberge
David Laberge

Reputation: 16051

Here is the PHP function I used to fix the issue:

rawurldecode(utf8_decode($_POST['myTextArea']))

Upvotes: 0

user399666
user399666

Reputation: 19879

You're probably not converting newlines into breaklines:

echo nl2br($_POST['myTextArea']);

And to make it safe for output:

echo nl2br( htmlentities( $_POST['myTextArea'], ENT_QUOTES, "utf-8" ) );

Upvotes: 4

raidenace
raidenace

Reputation: 12826

The enter keys are getting saved, but they are preserved as \n which will not show up as line breaks in HTML. You need to do an nl2br() while displaying the content back in the web page. This will convert the \n line break characters to <br> and show it correctly in a browser.

Upvotes: 2

Related Questions