Line break in textarea field PHP Mail form
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I have a textarea in form and when the user writes the following text:

"Hi,

My order is:

bread

milk

biscuits

juice"

I got all in one line

"Hi, My order is: bread milk biscuits juice"

I wish to keep every new line break that the user entered because when there is a lot of text in the textarea field, it is almost unreadable.

I will try to put nl2br somewhere in php code but didn't succeed. Because of my weak knowledge of php, please take the concrete steps I have to take.

Contact form: https://windor.hr/new/hr/kontakt.html

PHP files: https://www.dropbox.com/sh/zd3c1sqysir0fwq/AAB6cg0AtbtynNdWqgr8U2via?dl=0

Hi mashtullah, I don't use MySQL for this form.
Saaskoo 1 month ago
what database are you using?
mashtullah 1 month ago
Maybe I do not understand, but I should not set up any mySQL database in cPanel. Want to view some files?
Saaskoo 1 month ago
@Saasko yes if i can view the php code, will help
mashtullah 1 month ago
awarded to mashtullah

Crowdsource coding tasks.

4 Solutions


$subject = strip_tags($_POST['name']);
$message = nl2br(strip_tags($_POST['message']));
$from = strip_tags($_POST['email']);
$to = "recipient@mail.com";
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $to. "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers);

UPDATE:

Edit this line:

 $mail->Body = $letter;

to:

 $mail->Body = nl2br(strip_tags($letter));
Do I have to put this code or change current? And where?
Saaskoo 1 month ago
I think you should change your current code, but I can't tell you where is wrong your code if you don't show me the code.
Stefano Balzarotti 1 month ago
No, I get plain mail instead html with lot of line breaks and empty spaces. https://www.dropbox.com/s/9iwfcabjlw0r16e/Screen%20Shot%202017-08-16%20at%2010.44.25.png?dl=0
Saaskoo 1 month ago

Hi saasko,
i have created you the complete code with demo with a form to insert data and retrieve it in same format

you can see the code at my newly created git repo at following link:

https://github.com/g10dra/test_bounty.php/blob/master/test.php

base solution is written on 27 and 28 no line of above file link

//now use $description varriable in your email body

Winning solution

Here you go

on the file action.php on line 6, below the line if (!$_POST) exit; add this line
$_POST['message']=nl2br($_POST['message']);

save and it should work perfectly
UPDATE
on the line 29 below this line $post = post_array_check($_POST); add this line
$post['message']=nl2br($post['message']);
This should work now

@Saasko, try this out then let me know...
mashtullah 1 month ago
@Saasko, try again...
mashtullah 1 month ago
Thank you, I really appreciate your help! You won.
Saaskoo 1 month ago
@Saasko, Thanks mate...
mashtullah 1 month ago

without complexes here is your solution :p

Problem

textarea, define line breaks by "/r/n", but HTML didn't interpret that when reading it, and it handle it as a plain text.

Solution

the simple solution to fix this is, to replace the "/r/n" by the following "<br/>" which is the line break in HTML.

use this function and your problem will be fixed: str_replace("/r/n", "<br/>", $textarea);

Thanks for your time and tip, but @mashtullah just won.
Saaskoo 1 month ago
it's ok :) thanks for the reply ;)
Houcem B. A. Chlegou 1 month ago
Thank you for your participation :) Have a really nice day :)
Saaskoo 1 month ago
have a nice day too :)
Houcem B. A. Chlegou 1 month ago
View Timeline