0

我有这个 ajax 表单,最后我有一个数组中的值。代码在下面..但我在这些项目的 php 响应中一直为空。我想我做这个 .ajax 调用是错误的,任何想法都会有很大帮助。我也不知道在 .ajax 调用的成功部分写什么。那是我想念的吗?请看下面:

$('#submit_third').click(function(){
    //update progress bar
    $('#progress_text').html('100% Complete');
    $('#progress').css('width','339px');
    //prepare the fourth step

   var fields = new Array(
        $('#usernameReg').val(),
        $('#passwordReg').val(),
        $('#email').val(),
        $('#firstname').val(), 
        $('#lastname').val(),
        $('#city').val(),
        $('#phone').val(),
        categoryResult,
        $("#msg").val()                     
    );

    var tr = $('#fourth_step tr');
    tr.each(function(){
        //alert( fields[$(this).index()] )
        $(this).children('td:nth-child(2)').html(fields[$(this).index()]);
    });


    //slide steps
    $('#third_step').fadeOut(1000);
    $('#fourth_step').delay(1000).fadeIn(1000);          

    $('#submit_fourth').click(function(){
        //send information to server
        console.log(fields);
       $.ajax({
            type: "POST",
            data: 'fields',
            dataType: 'json',
            url: "php/postEngine.php",
            success: function(html){

            }
        });


    });     
});

php 信息(我正在使用蓬勃发展:

include_once($_SERVER['DOCUMENT_ROOT'] . '/../inc/init.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/dev/form_data.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/dev/db_handling.php');
ini_set('display_errors', 'On');
try {

    $tbl_user   =   'tbl_users';

    $statement = $db->prepare("INSERT INTO $tbl_user 
        (username,
        email,
        firstname,
        lastname,
        city,
        phone)
        VALUES 
        (%s,
        %s,
        %s,
        %s,
        %s,
        %s
        )"
    );

    $db->query($statement,
        $un,
        $email,
        $firstname,
        $lastname,
        $city,
        $phone
        );

    //  Update of Email_Replied to 1

    //$db->execute("UPDATE accommodation SET Email_Replied = '1' WHERE ID = %i", $user_id); 

} catch (Exception $e) {
    echo $error_msg;
    echo $un;
    echo $email;
    echo $firstname;
    echo $lastname;
    echo $city;
    echo $phone;
    echo json_encode($un);
    $error_msg = 'An error occured on postEngine.php';

}

?>

$un         =   fRequest::get('fields[0]');
$email      =   fRequest::get('fields[2]');
$firstname  =   fRequest::get('fields[3]');
$lastname   =   fRequest::get('fields[4]');
$city       =   fRequest::get('fields[5]');
$phone      =   fRequest::get('fields[6]');

?>

4

1 回答 1

6

它应该是 :

data: fields,

不是

data: 'fields',

现在您所做的只是将字符串“字段”发送到服务器,而不是变量fields

还要确保您期望服务器上有一个数组,并将 JSON 返回到客户端等,因为您在 中指定了 JSON dataType,但是在您的成功函数中,您将其称为 HTML,它不是,不是它会问题html只是一个变量,但看起来您并不期待 JSON,但仍然期待 JSON,并且当dataType设置为 JSON 时,任何包含无效 JSON(如 HTML)的响应都会失败?

于 2012-11-06T16:13:16.247 回答