我是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并回显“您已订阅我们的新闻通讯。谢谢,我们会联系你很快。“
提前致谢!
答案 0 :(得分:3)
输入字段需要name
属性才能与表单一起发送
<input type="text" name="email" id="email" placeholder="Your email address.." />
答案 1 :(得分:1)
您没有设置name
属性。试试这个:
<input type="text" name="email" placeholder="Your email address.." />
只是一个小建议:如果您只为自己发送电子邮件地址,某些地址可能会丢失。将电子邮件插入emails
数据库表,如下所示:
$email = $_POST['email'];
mysqli_query($conn, "INSERT INTO emails (email, time) VALUES ($email, CURRENT TIME())");