联系表格不会提交数据

时间:2014-08-21 15:11:24

标签: php json forms

我在我的Bootstrap 3联系表单的sendemail.php文件中有一个JSON代码。表单可以向指定的电子邮件地址发送电子邮件,但没有提交数据。我收到带有标签和标题'未知发件人'的空白电子邮件。请帮忙解决这个问题。

这是bootsrap表单代码块

<div class="col-sm-6">
                    <h1>Contact Form</h1>
                    <p>Please contact us using the form or our contact details are available here if you'd like to contact us via Email or Phone.</p>
                    <div class="status alert alert-success" style="display: none"></div>
                    <form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
                        <div class="row">
                            <div class="col-sm-6">
                                <div class="form-group">
                                    <input type="text" name="Name" class="form-control" required placeholder="Name">
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group">
                                    <input type="text" name="Email" class="form-control" required placeholder="Email address">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-12">
                                <div class="form-group">
                                    <textarea name="Message" id="message" required class="form-control" rows="8" placeholder="Message"></textarea>
                                </div>
                                <div class="form-group">
                                    <button type="submit" class="btn btn-danger btn-lg">Send Message</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div><!--/.col-sm-6-->

这是main.js代码,其中存在联系表单的AJAX

jQuery(function($) {

$(function(){
    $('#main-slider.carousel').carousel({
        interval: 5000,
        pause: false
    });
});

//Ajax contact
var form = $('.contact-form');
form.submit(function () {
    $this = $(this);
    $.post($(this).attr('action'), function(data) {
        $this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
    },'json');
    return false;
});

//smooth scroll
$('.navbar-nav > li').click(function(event) {
    if(!$( this ).attr( 'href' ).match(/^#/)) return; 
    event.preventDefault();
    var target = $(this).find('>a').prop('hash');
    $('html, body').animate({
        scrollTop: $(target).offset().top
    }, 500);
});

//scrollspy
$('[data-spy="scroll"]').each(function () {
    var $spy = $(this).scrollspy('refresh')
})

//PrettyPhoto
$("a.preview").prettyPhoto({
    social_tools: false
});

//Isotope
$(window).load(function(){
    $portfolio = $('.portfolio-items');
    $portfolio.isotope({
        itemSelector : 'li',
        layoutMode : 'fitRows'
    });
    $portfolio_selectors = $('.portfolio-filter >li>a');
    $portfolio_selectors.on('click', function(){
        $portfolio_selectors.removeClass('active');
        $(this).addClass('active');
        var selector = $(this).attr('data-filter');
        $portfolio.isotope({ filter: selector });
        return false;
    });
});
});

这是sendemail.php代码

<?php    
header('Content-type: application/json');
$status = array(
    'type'=>'success',
    'message'=>'Email sent!'
);

$name = $_POST['Name']; 
$email = $_POST['Email']; 
$subject = $_POST['Subject']; 
$message = $_POST['Message']; 

$email_from = $email;
$email_to = 'myemail@domain.com';

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

&GT;

2 个答案:

答案 0 :(得分:0)

您缺少html输入标记中的name属性

这是服务器端变量所必需的,例如:

$name = $_POST['Name'];

将从

获得价值
<input type="text" name="Name" class="form-control" required placeholder="Name">

<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <input name="Name" type="text" class="form-control" required placeholder="Name">
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                <input name="Email" type="text" class="form-control" required placeholder="Email address">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <div class="form-group">
                <textarea  name="Message" id="message" required class="form-control" rows="8" placeholder="Message"></textarea>
            </div>
            <div class="form-group">
                <input type="submit" class="btn btn-danger btn-lg" value="Send Message">
            </div>
        </div>
    </div>
</form>

<?php    
if(isset($_POST['submit'],$_POST['Name'],$_POST['Email'],$_POST['Subject'],$_POST['Message'])){
    $name = $_POST['Name']; 
    $email = $_POST['Email']; 
    $subject = $_POST['Subject']; 
    $message = $_POST['Message']; 

    $email_from = $email;
    $email_to = 'myemail@domain.com';

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

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

    $response = array();

    if($success){
        $response['success'] = true;
        $response['message'] = 'Email sent!';
    }else{
        $response['success'] = false;
        $response['message'] = 'Email sent!';
    }
}else{
    $response['success'] = false;
    $response['message'] = 'post variables are not set';
}

echo json_encode($response);
//die;

答案 1 :(得分:0)

在main.js中更改此行:

$.post($(this).attr('action'), function(data) {

为:

$.post($(this).attr('action'), $(this).serialize(), function(data) {

现在将提交数据。