Web Hosting Talk







View Full Version : HTML GET with PHP Multiline Varibles


Cyberoonie
08-01-2007, 07:26 AM
Hi,

I've only just started getting into PHP but have been involved with a web project to get my experience levels up.

What I have at the moment is a contact form that submits all its information to a php processing page. On the processing page it will *eventually* email the details to a specified E-Mail address but also display the information the user entered on the page as a confirmation.

What I'm having trouble with is displaying the multiline text box fields as multiline on the confirmation page. For example, on the contact form there is a "Any Other Information" field that is multiline. If a user enters information with line breaks or paragraphs these are not represented on the confirmation page.

I have imported all of the information into variables so have $other = $_GET['other'] etc. What I have noticed in the address bar is where the line breaks should be they are represended with the ASCII %0D%0A. Just trial and error I tried using str_replace to see if I could get it to change to a line break: $other = str_replace("%0D%0A", "<br />", $other); .. but that didnt work.

Anyone have any ideas?

Many thanks.

Azavia
08-01-2007, 08:00 AM
That is because line breaks in a textarea field are represented by either a linefeed or a carriage return followed by a linefeed (lf or cr lf), depending on the operating system.

This is why you saw %0d%0a. %0d is a carriage return and %0a is the linefeed, ascii 14 and 10 respectively. These characters (%0d and %0a) are decoded into the cr lf characters when parsed by PHP, so that is why replacing them didn't help.

If you would view the source of the page, you would see that in fact there are line breaks; they just aren't showing up in the HTML page. The reason is that in HTML, to actually show a line break, you must insert <br /> tags.

Thus I'd recommend the PHP function nl2br() (http://php.net/nl2br), which inserts a br tag before each line break.

Cyberoonie
08-01-2007, 08:31 AM
That worked like a charm, thank you!

Thanks for the explanation on it as well!

ThatScriptGuy
08-01-2007, 10:54 AM
Also, why don't you post the data to the script instead of getting it? I can't imagine why you'd want the whole content of a text box showing up in your address bar...