请原谅,我是新手,我尝试过的不同的php并没有真正起作用。我已经找到了自然语言形式的例子,但没有一个与工作的PHP和我熟悉的PHP,当使用传统的形式,但我有问题把两者放在一起。
一般的表单布局如下:
我的名字是[你的名字]。
今天,我要[你要做什么?]。
我这么做是因为[拜托,为什么?]。
我[你说过你会做的]很重要,因为[大局]。
给我发封邮件,把我的新宣言发到[你的邮箱]
按钮1(发送电子邮件)
按钮2(复制所有文本和输入字段到剪贴板-现在不那么重要)
按钮1,我想用硬编码的电子邮件地址发送一份给我自己,也用他们输入的电子邮件地址发送一份给用户。
现在有点乱,因为我只是想让它工作…同样,我没有包含PHP,因为在这一点上——我已经把自己弄糊涂了,我不知道该包含什么。
<form method="post" action="todayiam.php">
<div class="naturallanguageform">
<div class="nlfinner">
<p class="line">
<span class="copy">My name is </span>
<span class="inputcontainer"><input class="textinput" name="name" value="" placeholder="[your name]">.</span>
<span class="copy">Today, I am </span>
<span class="inputcontainer"><input class="textinput" name="todayiam" value="" placeholder="[whatchya gonna do?]">.</span>
<span class="copy">I'm doing this because </span>
<span class="inputcontainer"><input class="textinput" name="why" value="" placeholder="[c'mon, why?]">.</span>
<span class="copy"> It's important that I </span>
<span class="inputcontainer"><input class="textinput" name="whatusaid" value="" placeholder="[what you said you'd do]"></span>
<span class="copy"> because </span>
<span class="inputcontainer"><input class="textinput" name="because" value="" placeholder="[the big picture]">.</span>
</p>
<span class="copy">Shoot me an email with my awesome new declaration at</span>
<span class="inputcontainer"><input class="textinput" name="email" value="" placeholder="[your email]"></span>
<p class="line">
<button class="button">Send to E-mail</button>
</p>
<p class="line">
<button class="button">Copy to post in comments</button>
</p>
</div>
</div>
</form>
任何帮助都将是非常感谢的。
更新:<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['todayiam']);
// set here
$subject = "Contact form submitted!";
$to = 'email@gmail.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $email'r'n";
$headers .= "Content-type: text/html'r'n";
// send the email
mail($to, $subject, $body, $headers);
// redirect afterwords, if needed
header('Location: index.html');
?>
另一个更新我已经改变$headers = "From: $email'r'n";to $headers = "From: email@gmail.com"。"' r ' n";设置一个静态地址(我的支持电子邮件),电子邮件仍然被识别为来自CGI Mailer。我已经验证了这不是缓存问题,并且正在使用正确的文件。
<?php
if (isset($_POST['name'], $_POST['todayiam'], $_POST['why'], $_POST['whatusaid'], $_POST['because'], $_POST['email']) {
// We enter this statement only if all the fields has been properly defined, this to avoid getting undefined index
$name = $_POST['name'];
$todayIam = $_POST['todayiam'];
$why = $_POST['why'];
$whatYouSaid = $_POST['whatusaid'];
$because = $_POST['because'];
$email = $_POST['email'];
// We define the variables for using in mail()
$to = 'email@gmail.com';
$to .= ', '.$email; // You wanted them to recieve a copy
$subject = "Contact form submitted!";
// You can put a lot more headers, check out the mail() documentation
$headers = "From: email@gmail.com" . "'r'n" ;
$headers .= "Content-type: text/html'r'n";
// Compose a $message from all the variables
$message = "My name is $name. ";
$message .= "Today, I am $todayIam.";
$message .= "I'm doing this because $why.";
$message .= "It's important that I $whatYouSaid";
$message .= "because $because.";
if (mail($to, $subject, $message, $header)) {
// Mail was successfully sent! Do something here
}
}
?>
在你发布你的答案之前,我正在写这个脚本:
<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['todayiam']);
// set here
$subject = "Contact form submitted!";
$to = 'email@gmail.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $email'r'n";
$headers .= "Content-type: text/html'r'n";
// send the email
mail($to, $subject, $body, $headers);
// redirect afterwords, if needed
header('Location: index.html');
?>
邮件=美元削减(strip_tags ($ _POST['邮件']));字段正在提取用户的电子邮件,并使用它作为$header中所述的发送来源…所以它工作得很好。有了新的脚本,你张贴,我不能让它与我的硬编码电子邮件或用户的电子邮件作为FROM工作。一开始,我真的很想了解剧本之间的区别,为什么一个能成功,而另一个不行,但现在……我真的只是希望我的电子邮件被硬编码为FROM。我以后再担心这些差异。就像我之前说过的,我确实尝试过以各种不同的形式来实现它。我相信这是一些简单的东西,我是一个PHP新手。谢谢。
对,所以经过一些评论的讨论,我决定发布一个答案,给出更多的细节,这样更容易读。
您的PHP代码缺少$message
中所有字段的组合。这很容易做到,因为您可以在PHP中将变量放入字符串中。我来告诉你怎么做。我还将向您展示如何避免未定义索引。
<?php
if (isset($_POST['name'], $_POST['todayiam'], $_POST['why'], $_POST['whatusaid'], $_POST['because'], $_POST['email']) {
// We enter this statement only if all the fields has been properly defined, this to avoid getting undefined index
$name = $_POST['name'];
$todayIam = $_POST['todayiam'];
$why = $_POST['why'];
$whatYouSaid = $_POST['whatusaid'];
$because = $_POST['because'];
$email = $_POST['email'];
// We define the variables for using in mail()
$to = 'email@gmail.com';
$to .= ', '.$email; // You wanted them to recieve a copy
$subject = "Contact form submitted!";
// You can put a lot more headers, check out the mail() documentation
$headers = "From: $email'r'n";
$headers .= "Content-type: text/html'r'n";
// Compose a $message from all the variables
$message = "My name is $name. ";
$message .= "Today, I am $todayIam.";
$message .= "I'm doing this because $why.";
$message .= "It's important that I $whatYouSaid";
$message .= "because $because.";
if (mail($to, $subject, $message, $header)) {
// Mail was sucsessfullly sent! Do something here
}
}
?>