简体   繁体   English

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

[英]Getting my AJAX/PHP contact form to send email

I've looked at various solutions but I just can't get my contact form to work. 我已经研究过各种解决方案,但无法使我的联系表正常工作。 **The issue im having is that the email wont actually send out to me, everything works client side but I dont get the email. **问题是电子邮件实际上不会发送给我,一切都在客户端起作用,但我没有收到。 So I have come here to surely get the duplicate question label. 所以我来这里肯定是得到重复的问题标签。 Here is my code: 这是我的代码:

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

Here is my process.php 这是我的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);


}
?>

I am completely new to forms, so thanks in advance for the help. 我是表单的新手,所以在此先感谢您的帮助。 If there are any resources that you can suggest that would be great. 如果有任何资源,您可以建议那会很棒。 Thanks! 谢谢!

It looks like you just forgot to include an action in your form element. 看起来您只是忘记了在表单元素中包含操作。 (Unless your binding an onsubmit event somewhere else) (除非您在其他地方绑定了onsubmit事件)

Try 尝试

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

You haven't set any action in your form element. 您尚未在表单元素中设置任何操作。 You've to set the path of your "process.php" in form element like following: 您必须在表单元素中设置“ process.php”的路径,如下所示:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM