esafwan
esafwan

Reputation: 18029

newline in text area in html form doesn't remain once posted

I'm having a textarea in my contact form with php mail function. In php mail i have set the header to html.

But even if the user types like this

Line1....
Line2....

I'm getting in mail like this.

Line1....Line2....

What could be the reason?

Update: The text area is as simple as this.

<textarea id ="msg" name="message" cols="" rows="5" class="msg">Message</textarea>

Its posted to this script with jquery ajax function

<?php
$sub =  "Message Posted";
$email = "[email protected]";
$message = "<b>Message :</b><br/>" . $_REQUEST["msg"];
$message = wordwrap($message, 70);

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: [email protected]' . "\r\n" .
    'Reply-To: '. $_REQUEST["email"] . "\r\n" .
    'X-Mailer: PHP/' . phpversion();


// Send
mail($email, $sub, $message,$headers);
?>

But when getting in email all are in a single line. Even if you write in 2 line and submit.

Upvotes: 4

Views: 3797

Answers (2)

mfreiholz
mfreiholz

Reputation: 717

if you set your mail to HTML you should replace all line breaks with HTML
Tags.

I think the PHP function you need is: string nl2br ( string $string [, bool $is_xhtml = true ] )

Upvotes: 3

Ramiz Uddin
Ramiz Uddin

Reputation: 4259

<?php
//whatever you want to replace new lines with
$newLineCode = "<br/>";
$message = $_POST['myTextArea'] ; //unadulterad text we got via Post
$modifiedTextAreaText = ereg_replace( "\n", $newLineCode, $message);
echo " Result of Code Snippet " . $modifiedTextAreaText ;
?>

Upvotes: 0

Related Questions