我的表格里的代码有什么问题吗?


Is there anything wrong with the code in my form?

我正在使用这个教程为我的网站创建一个联系表单,它似乎不工作。在我填完表格并按下提交后,什么都没有发生。我想知道它出了什么问题,怎样才能修好它。

这是我的代码:

HTML:

    <form action="mail/contact.php" method="post">
                  <div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
  <label>Name:</label><br />
  <input type="text" name="name" id="name" class="form-control" placeholder="Name"/>
  </div></div>
<br />
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
  <label>Email:</label><br />
  <input type="text" name="email" id="email" class="form-control" placeholder="Email"/>
  </div></div>
<br />
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
  <label>Message:</label>
  <textarea name="message" rows="5" id="message" class="form-control" placeholder="Message"></textarea>
  </div></div>

<br />
  <input type="submit" name="submit" value="Submit" class="btn btn-success btn-lg"/>
</form>
PHP:

<?php
       // from the form
       $name = trim(strip_tags($_POST['name']));
       $email = trim(strip_tags($_POST['email']));
       $message = htmlentities($_POST['message']);
       // set here
       $subject = "Contact form submitted!";
       $to = 'name@mail.com';
       $body = <<<HTML
$message
HTML;
       $headers = "From: $email'r'n";
       $headers .= "Content-type: text/html'r'n";
       // send the email
       mail($to, $subject, $body, $headers);
       // popup success
       echo '<script language="javascript">';
       echo 'alert("Your message has been sent!")';
       echo '</script>';
?>

这实际上不是问题,您必须在email()方法中将$to更改为$email,因为包含您要发送的电子邮件的变量是$email。

您的contact.php代码将是:

<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$body = <<<HTML
$message
HTML;
$headers = "From: $email'r'n";
$headers .= "Content-type: text/html'r'n";
// send the email
mail($email, $subject, $body, $headers);
// popup success
echo '<script language="javascript">';
echo 'alert("Your message has been sent!")';
echo '</script>';
?>

另一个建议是查看本教程中的安全联系表单。

你也应该看看这两个php框架:

  • Symfony 2 Mail
  • Zend 2 Mail