【问题标题】:HTML/PHP Newsletter formHTML/PHP 通讯表格
【发布时间】: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="" - 打开错误报告,您会收到“未定义索引”错误
  • HTML Form POST is null的可能重复

标签: php html forms


【解决方案1】:

输入字段需要name 属性才能与您的表单一起发送

&lt;input type="text" name="email" id="email" placeholder="Your email address.." /&gt;

【讨论】:

    【解决方案2】:

    您没有设置name 属性。试试这个:

    &lt;input type="text" name="email" placeholder="Your email address.." /&gt;

    还有一个小建议:如果您只为自己发送电子邮件地址,则某些地址可能会丢失。将电子邮件插入到emails 数据库表中,如下所示:

    $email = $_POST['email'];
    mysqli_query($conn, "INSERT INTO emails (email, time) VALUES ($email, CURRENT TIME())");
    

    【讨论】:

      猜你喜欢
      • 2013-09-01
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      • 2011-08-15
      • 2016-08-26
      • 2012-10-09
      • 2022-09-27
      • 1970-01-01
      相关资源
      最近更新 更多