php联系表单发送没有信息的电子邮件

时间:2016-08-25 04:37:23

标签: javascript php email

我收到了我网站联系表格中的电子邮件,但没有收到任何内容。

这是我的代码:

PHP

<?php

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

// recipient address
$to = "admin@deptfordtechservices.com";

// subject of email
$re = "New Inquiry";

// message creation
$contact = "Name:".$name."\nEmail:".$from."\nSubject:".$subject."\r\n";

$txt = "Message:".$message."\r\n";


$fmsg = $contact."\r\n".$txt;

$msg = wordwrap($fmsg, 70);

// send email
mail($to,$re,$msg);

die;

HTML

<form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
    <div class="form-group">
        <input type="text" name="name" id="name" class="form-control" placeholder="Name" required>
    </div>
    <div class="form-group">
        <input type="email" name="email" id="email" class="form-control" placeholder="Email" required>
    </div>
    <div class="form-group">
        <input type="text" name="subject" id="subject" class="form-control" placeholder="Subject" required>
    </div>
    <div class="form-group">
        <textarea name="message" id="message" class="form-control" rows="8" placeholder="Message" required></textarea>
    </div>
    <button type="submit" class="btn btn-primary">Send Message</button>
</form>

的Javascript

// 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();
    });
});

网站:deptfordtechservices.com

提前致谢,

大卫

1 个答案:

答案 0 :(得分:3)

Javascript不发送表单输入。它需要data:选项来提供参数,并且需要使用type: 'POST',因此它会发送POST而不是GET

// 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'),
        type: 'POST',
        data: $(this).serialize(),
        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();
    });
});