获取我的AJAX / PHP联系表单以发送电子邮件

时间:2014-07-07 18:45:15

标签: php ajax

我已经查看了各种解决方案,但我无法让我的联系表单正常工作。 **我遇到的问题是电子邮件实际上不会发送给我,一切都在客户端,但我没有收到电子邮件。所以我来到这里肯定会得到重复的问题标签。这是我的代码:

<form method="post" class="reply" id="contact" action="process.php">
                       <fieldset>
                          <div class="row">
                             <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                                <label>Name: <span>*</span></label>
                                <input class="form-control" id="name" name="name" type="text" value="" required>
                             </div>
                             <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                                <label>Email: <span>*</span></label>
                                <input class="form-control" type="email" id="email" name="email" value="" required>
                             </div>
                          </div>
                          <div class="row">
                             <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                                <label>Subject: <span>*</span></label>
                                <input class="form-control" id="subject" name="subject" type="text" value="" required>
                             </div>
                          </div>
                          <div class="row">
                             <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                                <label>Message: <span>*</span></label>
                                <textarea class="form-control" id="text" name="text" rows="3" cols="40" required></textarea>
                             </div>
                          </div>
                       </fieldset>
                       <button class="btn btn-normal btn-color submit  bottom-pad" type="submit">Send</button>
                       <div class="success alert-success alert" style="display:none">Your message has been sent successfully.</div>
                       <div class="error alert-error alert" style="display:none">E-mail must be valid and message must be longer than 100 characters.</div>
                       <div class="clearfix">
                       </div>
                    </form>

这是我的process.php

<?php
// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['subject']) &&     isset($_POST['text']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {

// detect & prevent header injections
$test = "/(content-type|bcc:|cc:|to:)/i";
foreach ( $_POST as $key => $val ) {
if ( preg_match( $test, $val ) ) {
  exit;
}
}


        // PREPARE THE BODY OF THE MESSAGE

        $message = '<html><body>';
        $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
        $message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
        $message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
        $message .= "<tr><td><strong>Message:</strong> </td><td>" . htmlentities($_POST['text']) . "</td></tr>";
        $message .= "</table>";
        $message .= "</body></html>";



        //   CHANGE THE BELOW VARIABLES TO YOUR NEEDS

        $to = 'iknowichange@this.com';

        $subject = $_POST['subject'];

        $headers = "From: " . $_POST['email'] . "\r\n";
        $headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

        mail($to, $subject, $message, $headers);


}
?>

我对形式完全不熟悉,所以提前感谢您的帮助。如果您有任何可以建议的资源,那就太棒了。谢谢!

2 个答案:

答案 0 :(得分:0)

看起来你只是忘了在表单元素中包含一个动作。 (除非你在其他地方绑定了onsubmit事件)

尝试

<form method="post" class="reply" id="contact" action="process.php">

答案 1 :(得分:0)

您尚未在表单元素中设置任何操作。您可以设置&#34; process.php&#34;的路径。在表格元素如下:

<form action="process.php" method="post" id="contact" class="reply">
    ...
</form>

有关表单的更多信息:http://www.w3schools.com/html/html_forms.asp