简体   繁体   English

如何在表单返回段落(PHP)中制作“textarea”

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

Beginner here. 初学者在这里。

I have a form with a textarea to return comments. 我有一个带有textarea的表单来返回评论。 I got a PHP script from helpvid.net to send the form to. 我从helpvid.net获得了一个PHP脚本来发送表单。 So, the form is being sent to a PHP file.The form then returns the information to me in an email. 因此,表单被发送到PHP文件。然后表单通过电子邮件将信息返回给我。

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. 即使用户在字段中点击Return以创建新行,电子邮件中返回的文本也在一行上。 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: 以下是返回注释的PHP文件的代码形式:

$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? 有没有办法修改此代码,或我的HTML文件返回段落?

nl2br是你的朋友:

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

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

This should do it. 这应该做到这一点。 Essentially it will turn line breaks into <br /> tags. 基本上它会将换行符转换为<br />标记。

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. 我知道Outlook有时会这样做。 Something worth checking... 值得检查的东西......

UPDATE UPDATE

Yes, the nl2br() will work in your case. 是的, nl2br()将适用于您的情况。 But if you removed your original <br> tags and just used \\n or hard line breaks you wouldn't need to use this function. 但是,如果您删除了原始的<br>标签并且只使用了\\n或硬换行符,则不需要使用此功能。 The <br> tags are the only HTML in your email. <br>标签是电子邮件中唯一的HTML标签。 It's not well-formed. 它没有良好的形成。 You'd be better off removing them and the line $headers .= "Content-type: text/html\\r\\n"; 你最好删除它们和行$headers .= "Content-type: text/html\\r\\n"; so you just send a plain/text email. 所以你只需发一封普通/短信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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