我的表单中的代码有什么问题吗?

时间:2015-08-22 03:04:42

标签: php html email post

我正在使用此tutorial为我的网站创建一个联系表单,但它似乎不起作用。在我填写表格并按提交后,没有任何反应。我想知道它有什么问题以及如何解决它。

这是我的代码:

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>';

?>

1 个答案:

答案 0 :(得分:0)

实际上这不是问题,您需要在$to方法中将$email更改为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>';

?>

另一项建议是检查this tutorial是否有安全的联系表单。

另外你应该看看这两个php框架: