PHP和AJAX发送数据时出现问题

时间:2014-02-18 12:01:58

标签: php ajax

初学者的另一个愚蠢的问题。我有这个功能:

    $(document).ready(function() {
    $('#submit-form').click(function(e){

        e.preventDefault();
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        var name     = $('#name').val(),
            email    = $('#email').val(),
            phone    = $('#phone').val(),
            date     = $('#date').val(),
            message  = $('#message').val(),
            data_html,
            success = $('#success');

        if(name == "")
            $('#name').val('Please enter your name.');

        if(phone == "")
            $('#phone').val('Please enter your phone number.');

        if(date == "")
            $('#date').val('Please enter a date and time.');

        if(email == ""){
            $('#email').val('Your email is required.');
        }else if(reg.test(email) == false){
            $('#email').val('Invalid Email Address.');
        }

        if(message == "")
            $('#message').val('Message is required.');

        if(message != "" && name != "" && reg.test(email) != false) {
            data_html = "name=" + name + "&email="+ email + "&message=" + message + "&phone="+ phone + "&date="+ date;

            //alert(data_html);
            $.ajax({
                type: 'POST',
                url: '../contact_form.php',
                data: data_html,
                success: function(msg){

                    if (msg == 'sent'){
                        success.html('<div class="alert alert-success">Message <strong>successfully</strong> sent!</div>')  ;
                        $('#name').val('');
                        $('#phone').val('');
                        $('#email').val('');
                        $('#date').val('');
                        $('#message').val('');

                    }else{
                      success.html('<div class="alert alert-error">Message <strong>NOT</strong> sent!  Please try again later. </div>')  ; 

                    }
                }
            });

        }
        return false;
   });
});

我已经创建了这个PHP可能是错的,所以请不要评判我...我还在学习:)我是一个初学者,所以请不要给我一个艰难的时间:))

 <?php


$to = 'dvvsfb1@gmail.com';

$subject = 'Request a Booking';


if($to) {
$name = $_POST['name'];
$email = $_POST['email'];

$fields = array(
    0 => array(
        'text' => 'Name',
        'val' => $_POST['name']
    ),
    1 => array(
        'text' => 'Email address',
        'val' => $_POST['email']
    ),
    2 => array(
        'text' => 'Phone',
        'val' => $_POST['phone']
    ),
    3 => array(
        'text' => 'Date & Time',
        'val' => $_POST['date']
    ),
    4 => array(
        'text' => 'Message',
        'val' => $_POST['message']
    )
);

$message = "";

foreach($fields as $field) {
    $message .= $field['text'].": " . htmlspecialchars($field['val'],       ENT_QUOTES) . "<br>\n";
}
ini_set("SMTP","aspmx.l.google.com");
$headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=utf-8\r\n";
$headers .= "From: \"" . $name . "\" \r\n";
$headers .= "Reply-To: " .  $email . "\r\n";
$message = utf8_decode($message);

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

&GT;

我一直收到消息未发送的错误消息。我觉得它与我的PHP无关。有什么建议吗?

2 个答案:

答案 0 :(得分:1)

您没有从PHP文件中“回答”任何内容。尝试使用以下命令替换“mail()”调用:

if (mail($to, $subject, $message, $headers)) {
    echo 'sent';
} else {
    echo 'not sent';
}

答案 1 :(得分:0)

在ajax成功回调中,您正在检查

if (msg == 'sent')

其中msg是ajax请求的响应响应。

但是在你的php文件中,你没有发送任何回复。尝试,

$status = mail($to, $subject, $message, $headers);
echo $status ? 'sent' : 'failed';

在Ajax成功回调中,

if($.trim(msg) == 'sent') {
    //some code
}