无法使用Jquery Ajax打印成功消息

时间:2014-02-18 01:34:50

标签: javascript php jquery

我正在使用Jquery Ajax发布表单数据并显示成功消息。一切正常,但我无法显示成功消息。以下是代码: 的的Javascript

<script>
$(document).ready(function() {
    $('form').submit(function(event) { //Trigger on form submit                 
        $('#stage').empty();

        var postForm = { //Fetch form data
            "name": $("#name").val(),
            "element_4_1": $("#element_4_1").val(),
            "element_4_2": $("#element_4_2").val(),
            "element_4_3": $("#element_4_3").val(),
            "email": $("#email").val(),
            "input4": $("#input4").val(),
        };

        $.ajax({ //Process the form using $.ajax()
            type        : 'POST', //Method type
            url         : 'contact.php', //Your form processing file url
            data        : postForm, //Forms name
            dataType    : 'json',
            success     : function(data) {
                console.log("inside success3") ;
                alert(data);
                $("#stage").html(data);

                if (!data.success) { //If fails
                    if (data.errors) { //Returned if any error from process.php
                        $('.throw_error').fadeIn(1000).html(data.errors); //Throw relevant error
                        console.log("inside failure") ;
                    }
                } else {
                    console.log("inside success") ;
                    $('#stage').fadeIn(1000).append('<p>' + data.posted + '</p>'); 
                    console.log("inside success2") ;
                }
            }
        });
        event.preventDefault(); //Prevent the default submit
    });
});
</script>  

PHP:

<?php

ini_set('display_errors','On');
error_reporting(E_ALL);
$errors = array(); 
$form_data = array(); 
header('Content-type: application/json');
echo json_encode($form_data);

$name=$_POST['name'];
$phone=chop($_POST['element_4_1']);
$phone.=chop($_POST['element_4_2']);
$phone.=chop($_POST['element_4_3']);
$email=chop($_POST['email']);
$message1=chop($_POST['input4']);

if ($name && $phone && $email) { 

    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
    $headers .= "From: sales@test.com \n";
    $recipient= "test@test.in";
    $subject="Online Enquiry   ";
    $message="\nName                   : $name\n";
    $message.="\nPhone                  : $phone\n";
    $message.="\nEmail ID               : $email\n";
    $message.="\nMessage                : $message1\n";

    //send auto-reply
    $subject_reply="Thank you for contacting us";
    $message_reply="Thank you for contacting us. We will get back to you shortly.";
    //mail($email, $subject_reply, $message_reply, $headers);

    //Send Mail
    //===========
    if(isset($recipient, $subject, $message, $headers)) {
        error_log($message);
        $form_data['status'] = 'success';
        error_log($form_data['status']);

    } else {
        $form_data['status'] = 'error';
        error_log($form_data['status']);

    } ?>  

HTML

<div id="stage">
</div>  

如何打印成功消息

1 个答案:

答案 0 :(得分:2)

你在php脚本的开头有这个:

echo json_encode($form_data);

其中$form_data当时是一个空数组。

你应该删除它并把它放在最后。