【问题标题】:Subscription form not working (PHP)订阅表格不起作用(PHP)
【发布时间】:2014-02-13 00:15:33
【问题描述】:

我试图让访问我网站的人通过填写一份简短的表格并让网站向我发送一封包含信息的电子邮件来订阅,但目前该表格甚至不会提交信息。一些帮助将不胜感激。

<form id="ContactForm" action="mail/MailHandler.php">
  <div class="success">Form submitted!<br><strong>Look forward to our next Monthly Mailer.</strong></div>
  <fieldset>
    <div class="wrapper">
      <label class="name">
        <span class="bg"><input type="text" value="Name:" class="input"></span>
        <span class="error">*This is not a valid name.</span> 
        <span class="empty">*This field is required.</span> 
      </label>
    </div>
    <div class="wrapper">
      <label class="email">
        <span class="bg"><input type="text" value="E-mail:" class="input"></span>
        <span class="error">*This is not a valid email address.</span> 
        <span class="empty">*This field is required.</span>
      </label>
    </div>
    <div class="btns">
      <a href="mail/MailHandler.php" class="button1" data-type="submit">
        <span><strong>submit</strong></span>
        <span class="active"><strong>submit</strong></span>
      </a>
    </div>
  </fieldset>
</form>

用PHP如下

<?php
$owner_email = $_POST["myemail@address.com"];
$headers = 'From:' . $_POST["email"];
$subject = 'Monthly Mailer Subscriber ' . $_POST["name"];
$messageBody = "";

$messageBody .= '<p>' . $_POST["name"] . ' would like to be subscribed to your Monthly Mailer!</p>' . "\n";
$messageBody .= '<br>' . "\n";
$messageBody .= '<p>Email Address: ' . $_POST['email'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";

try{
    if(!mail($owner_email, $subject, $messageBody, $headers)){
        throw new Exception('mail failed');
    }else{
        echo 'mail sent';
    }
}catch(Exception $e){
    echo $e->getMessage() ."\n";
}
?>

问题已解决!问题在于我的输入没有名称,并且 $owner_email = $_POST["myemail@address.com"]; 本来应该 $owner_email = 'myemail@address.com';

【问题讨论】:

  • PHP中的邮件功能不可靠,尝试使用SwiftMailer之类的PHP邮件类

标签: php html email subscription


【解决方案1】:

问题 #1

您正在寻找POST 变量,但通过GET 发送它们,因为您从未将其明确设置为POST。要解决此更改:

<form id="ContactForm" action="mail/MailHandler.php">

到:

<form id="ContactForm" action="mail/MailHandler.php" method="POST">

问题 #2

您忘记提供输入名称:

<input type="text" value="Name:" class="input">

应该是:

<input type="text" value="Name:" name="name" class="input">

问题 #3

您缺少提交按钮:

<input type="submit" value="submit">

应该替换:

<a href="mail/MailHandler.php" class="button1" data-type="submit">
    <span><strong>submit</strong></span>
    <span class="active"><strong>submit</strong></span>
  </a>

【讨论】:

  • 我试过这个,但没有骰子。我不完全确定 php 是如何工作的,但是我需要某种 js 来配合这些吗?
  • 不需要 javascript 就可以工作。按下提交按钮时会发生任何事情吗?如果不是,您的 HTML 在某种程度上是无效的。
  • 哦!我让它做某事...我让它抛出“邮件失败”异常...至少我知道它现在正在提交一些东西。
  • 这里是网站以防万一。表格位于页面底部。 cisnerosdesignstudio.com/#!/page_monthly_mailers
  • 问题解决了!问题在于我的输入没有名称,$owner_email = $_POST["myemail@address.com"]; 的语法应该是 $owner_email = 'myemail@address.com';
【解决方案2】:

您没有设置表单元素名称。

您需要将输入更改为以下内容:

    <input type="text" value="E-mail:" name="email" class="input">
    <input type="text" value="Name:" name="name" class="input">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-05
    • 2014-08-10
    • 1970-01-01
    相关资源
    最近更新 更多