【发布时间】:2017-08-07 11:18:36
【问题描述】:
我对 PHP 还很陌生,我正在尝试使电子邮件提交(时事通讯)表单工作。 我有以下 HTML 表单:
<form class="form-inline" action="mail-handler.php" method="post">
<div class="col-sm-5 col-sm-offset-2 col-md-4 col-md-offset-3">
<label class="sr-only" for="email">Email</label>
<input type="text" id="email" placeholder="Your email address.." />
</div>
<div class="col-sm-3 col-md-2">
<input type="submit" name="submit" class="btn btn-submit" value="Submit">
</div>
</form>
为此,我创建了一个带有以下代码的 mail-handler.php 文件:
<?php
$to = "horia.imperial@gmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$subject = "Form submission";
$subject2 = "You have just subscribed to the Plantsnap newsletter!";
$message = $from . " just subscribed for your Newsletter" ;
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
//Headers
$headers .= "From: <".$from. ">";
$headers2 = "From:" . $to;
if(isset($_POST['submit'])){
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "You have subscribed to our newsletter. Thank you, we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
// You cannot use header and echo together. It's one or the other.
} ?>
问题: 当我按下提交按钮时,它会向我的地址发送一封电子邮件,但我收到以下电子邮件:
来自: 至:horia.imperial@gmail.com 日期:2017 年 3 月 16 日星期四下午 12:23 主题:表单提交 加密:标准 (TLS) 了解详情
如您所见,from: 字段为空,这意味着无论我在输入字段中输入什么,$from = $_POST['email'] 都会返回 NULL。
我做错了什么?
另外,我还有一个问题:
如何使来自表单提交的消息显示为表单下方的小工具提示,而不是将我重定向到 link/mail-handler.php 并回显“您已订阅我们的时事通讯。谢谢,我们会联系你很快就会。”
提前致谢!
【问题讨论】:
-
您尚未命名输入字段,您需要
name=""而不是id=""- 打开错误报告,您会收到“未定义索引”错误