在PHP联系表单中换行 [英] Line breaks in a PHP contact form

查看:103
本文介绍了在PHP联系表单中换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我正在创建的网站添加一个联系表单,并发现有人为我这类没有PHP经验的人分享了一些有用的代码。



在将代码粘贴并编辑到页面并上传到暂存站点后,我使用我的Gmail地址对其进行了测试,结果正常。只有电子邮件文本没有换行符。



以下是代码:


$ ($ _POST [email]<>''){
$ ToEmail ='myemailadress @ gmail.com;
$ EmailSubject ='网站联系表';
$ mailheader =From:。$ _ POST [email]。\r\\\
;
$ mailheader。=回复:。$ _ POST [email]。\r\\\
;
$ mailheader。=Content-type:text / html; charset = iso-8859-1 \r\\\
;
$ MESSAGE_BODY =Name:。$ _ POST [name]。;
$ MESSAGE_BODY。=电子邮件:。$ _ POST [email]。;
$ MESSAGE_BODY。=Comment:.nl2br($ _ POST [comment])。;
邮件($ ToEmail,$ EmailSubject,$ MESSAGE_BODY,$ mailheader)或死(失败);
?>
您的消息已发送
<?php
}其他(
?>
< form action =contactus.phpmethod =post>
< table width =400border =0cellspacing =2cellpadding =0>
< tr>
< td width =29%class =bodytext>您的名字:< / td>
< td width =71%>< input name =nametype =textid =namesize =32>< / td>
< / tr>
< tr>
< td class =bodytext>电子邮件地址:< / td>
< td>< input name =emailtype =textid =emailsize =32>< / td>
< / tr>
< tr>
< td class =bodytext>评论:< / td>
< td>< textarea name =commentcols =45rows =6id =commentclass =bodytext> < / textarea的>< / TD>
< / tr>
< tr>
< td class =bodytext> < / TD>
< td align =leftvalign =top>< input type =submitname =Submitvalue =Send>< / td>
< / tr>
< / table>
< / form>
<?php
};
?>

这是我收到邮件时收到的内容:
lockquote

姓名:Mr J BloggsEmail:email@address.com评论:评论内容。


我想让它看起来像这样:


姓名:J Bloggs先生

电子邮件:email@address.com

评论:评论文字


感谢您提供任何帮助。

  $ MESSAGE_BODY =Name:。$ _ POST [name]。< br />; 
$ MESSAGE_BODY。=Email:。$ _ POST [email]。< br />;
$ MESSAGE_BODY。=评论:.nl2br($ _ POST [comment]);


I need to add a contact form to a website I'm making, and found a useful bit of code someone had kindly shared for the likes of me who have no PHP experience.

After pasting and editing the code into the page and uploading it to a staging site, I tested it using my gmail address and it works fine. The only thing is that the email text has no line breaks in it.

Here's the code:

<?php 
if ($_POST["email"]<>'') { 
$ToEmail = 'myemailadress@gmail.com'; 
$EmailSubject = 'Site contact form'; 
$mailheader = "From: ".$_POST["email"]."\r\n"; 
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
$MESSAGE_BODY = "Name: ".$_POST["name"].""; 
$MESSAGE_BODY .= "Email: ".$_POST["email"].""; 
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"]).""; 
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
?>
Your message was sent
<?php 
} else { 
?>
<form action="contactus.php" method="post">
<table width="400" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="29%" class="bodytext">Your name:</td>
<td width="71%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td class="bodytext">Email address:</td>
<td><input name="email" type="text" id="email" size="32"></td>
</tr>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"> </textarea></td>
</tr>
<tr>
<td class="bodytext"> </td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form> 
<?php 
}; 
?>

This is what I receive when it arrives in my inbox:

Name: Mr J BloggsEmail: email@address.comComment: Comments text.

What I'd like is for it to instead look like this:

Name: Mr J Bloggs
Email: email@address.com
Comment: Comments text.

Thanks in advance for any help.

解决方案

Just add line break after each row:

$MESSAGE_BODY = "Name: ".$_POST["name"]."<br/>"; 
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br/>"; 
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"]); 

这篇关于在PHP联系表单中换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆