联系表格不起作用

时间:2016-11-30 07:57:06

标签: php html forms

我购买了一个模板,但没有包含邮件文件,我试图让这个工作。这是形式:

    <div class="row">
      <div class="col-sm-6">
        <div class="well">
          <form id="contactForm" data-toggle="validator" class="shake" action="mailer.php" method="post">

            <div class="form-group"><!-- ENTER MANE -->
              <input type="text" name="name" class="form-control" id="name" placeholder="Enter name" required data-error="NEW ERROR MESSAGE">
              <div class="help-block with-errors"></div>
            </div>

            <div class="form-group"><!-- ENTER EMAIL -->
              <input type="email" name="email" class="form-control" id="email" placeholder="Enter email" required>
              <div class="help-block with-errors"></div>
            </div>

            <div class="form-group"><!-- ENTER MESSAGE -->
              <textarea id="message" name="message" class="form-control" rows="5" placeholder="Enter your message" required></textarea>
              <div class="help-block with-errors"></div>
            </div>

            <!-- button form submit -->
            <div class="submit">
<!--               <button id="form-submit" name="submitted" class="btn btn-default btn-lg">Submit</button> -->
              <input id="form-submit" name="submitted" class="btn btn-default btn-lg" type="submit" value="Submit">
              <div id="msgSubmit" class="h5 h5 text-danger hidden"></div>
              <div class="clearfix"></div>
            </div>

          </form>

这是我用不同模板购买的旧邮件文件:

<?php
$email_to = "example@domain.com"; // write your email here, or email where it has to be sent
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$text = "NAME: $name<br>
         EMAIL: $email<br>   
         MESSAGE: $message";
mail($email_to, "Subject", $text);
?>

为什么这不起作用?

1 个答案:

答案 0 :(得分:1)

似乎应该发送的电子邮件是错误的。使用您希望接收邮件的电子邮件正确设置$email_to变量。

同样使用mail(),您无需设置标头。你可以简化它:

<?php
$email_to = "example@domain.com"; // write your email here, or email where it has to be sent
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$text = "NAME: $name<br>
         EMAIL: $email<br>   
         MESSAGE: $message";
mail($email_to, "Subject", $text);
?>

此外,我不会在您的案例验证器中使用您不理解的内容并从其他网站获取。

HTML中的按钮可能存在问题,因为提交应该由JavaScript完成。如果没有,你应该改变

<button id="form-submit" name="submitted" class="btn btn-default btn-lg">Submit</button>

<input id="form-submit" name="submitted" class="btn btn-default btn-lg" type="submit" value="Submit">

<强>调试

如果您收到空消息,请尝试:

echo "<pre>";
print_r($_POST);

检查$_POST数组内的内容。

<强>手册

PHP: mail