简体   繁体   中英

How to make “textarea” in Form return paragraphs (PHP)

Beginner here.

I have a form with a textarea to return comments. I got a PHP script from helpvid.net to send the form to. So, the form is being sent to a PHP file.The form then returns the information to me in an email.

Problem is that the comments are sent as one large run-in paragraph. Even if the user hits Return in the field to make a new line, the text returned in the email is on one line. If the user hits Return twice to make a new paragraph, those paragraphs are returned as one long paragraph. I would like the text returned to have the line breaks that the user puts in.

Here is the code form the PHP file that is returning the comments:

$name = $_POST['name'];
$company = $_POST['company'];
$address = $_POST['address'];
$city = $_POST['city'];

$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$optin = $_POST['optin'];
$comments = $_POST['comments'];



$body = <<<EOD
<br><br>
Please send samples to: <br /><br />
$name <br />
$company <br />
$address <br>
$city, $state $zip <br><br />
Email: $email <br><br />
Opt-In to occasional email list?: $optin <br><br />
Comments: $comments <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body,
$headers);

Is there a way to modify this code, or my HTML file to return the paragraph breaks?

nl2br是你的朋友:

$comments = nl2br($_POST['comments']);

http://www.php.net/manual/en/function.nl2br.php

This should do it. Essentially it will turn line breaks into <br /> tags.

Note that if you are indeed sending plain text emails, this shouldn't be a problem. Are you sure your email client isn't stripping the line breaks? I know Outlook does this sometimes. Something worth checking...

UPDATE

Yes, the nl2br() will work in your case. But if you removed your original <br> tags and just used \\n or hard line breaks you wouldn't need to use this function. The <br> tags are the only HTML in your email. It's not well-formed. You'd be better off removing them and the line $headers .= "Content-type: text/html\\r\\n"; so you just send a plain/text email.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM