网站联系表单不接收消息

时间:2016-01-17 00:54:23

标签: javascript php html contact-form

非常新的HTML和网站开发人员,不确定为什么我当前的联系人框无效。我正在托管我自己的网站,当我尝试发送时,我收到一条消息发送回复但电子邮件从未收到过。

我没有通过使用多个电子邮件地址来发送和接收来消除电子邮件托管被破坏但是无法找到问题的根源。

见下文相关代码

main.js

// Contact form
var form = $('#main-contact-form');
form.submit(function (event) {
    event.preventDefault();
    var form_status = $('<div class="form_status"></div>');
    $.ajax({
        url: $(this).attr('action'),
        beforeSend: function () {
            form.prepend(form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn());
        }
    }).done(function (data) {
        form_status.html('<p class="text-success">Thank you for contact us. As early as possible  we will contact you</p>').delay(3000).fadeOut();
    });
});

sendemail.php

<?php
$name       = @trim(stripslashes($_POST['name'])); 
$from       = @trim(stripslashes($_POST['email'])); 
$subject    = @trim(stripslashes($_POST['name']));  
$message    = @trim(stripslashes($_POST['message'])); 
$to         = '\\myemailaddress//';

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();

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

die;

的index.html

<div class="col-md-8 col-sm-12">
     <div class="contact-form bottom">
           <h2>Send a message</h2>
                <form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
                     <div class="form-group">
                         <input type="text" name="name" class="form-control" required="required" placeholder="Name">
                        </div>
                        <div class="form-group">
                            <input type="email" name="email" class="form-control" required="required" placeholder="Email Id">
                        </div>
                        <div class="form-group">
                            <textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Your text here"></textarea>
                        </div>
                        <div class="form-group">
                            <input type="submit" name="submit" class="btn btn-submit" value="Submit">
                       </div>
               </form>
       </div>
</div>

提前谢谢你!

2 个答案:

答案 0 :(得分:1)

下一个问题是你没有用你的ajax发送任何数据。使用$(formSelector).serialize()简化了收集数据

$.ajax({
    url: $(this).attr('action'),
    //serialize form data and include it with request
    data: form.serialize(),
    beforeSend: function () {
        ...
    }
}).done(function (data) {
    ....
});

目前,如果你在php中print_r($_POST);,你会发现它是空的

答案 1 :(得分:0)

从@charlietfl回答后工作。更改类型和网址似乎解决了它。 下面的工作代码:

main.js:

var form = $('#main-contact-form').serialize();
form.submit(function (event) {
    event.preventDefault();
    var form_status = $('<div class="form_status"></div>');
    $.ajax({
        url: $(this).attr('action'),
        type: 'POST',
        dataType: "json",
        beforeSend: function () {
            form.prepend(form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn());
        }
    }).done(function (data) {
        form_status.html('<p class="text-success">' + data.message + '</p>').delay(3000).fadeOut();

    });
});

和我的.php

<?php 
    header('Content-type: '); 
    $status = array( 
        'type'=>'success', 
        'message'=>'Thank you for contact us. As early as possible  we will contact you ' 
    ); 

$name = @trim(stripslashes($_POST['name']));  
$email = @trim(stripslashes($_POST['email']));  
$subject = @trim(stripslashes($_POST['subject']));  
$message = @trim(stripslashes($_POST['message']));  

$email_from = $email; 
$email_to = '\\myemail//'; 

$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message; 

$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>'); 

echo json_encode($status); 
die;