我有这个ajax表单,最后我在数组中有值。代码在下面..但我在这些项目的php响应中一直变为null。我想我这样做.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 info(我正在使用蓬勃发展:
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';
}
&GT?;
$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]');
>
答案 0 :(得分:6)
应该是:
data: fields,
不是
data: 'fields',
现在你要做的就是将字符串'fields'发送到服务器,而不是变量fields
?
同时确保您期望服务器上有一个数组,并且JSON会回到客户端等,因为您在dataType
中指定了JSON,但在您的成功函数中,您将其称为HTML,它不是,并不重要,因为html
只是一个变量,但它确实看起来你不期望JSON,但仍然期待JSON,当dataType
设置为JSON时,任何包含无效JSON(如HTML)的响应都会失败?