我无法将带有AJAX的JSON数组发送到PHP文件,该文件将信息插入数据库中的正确列。 jquery和Json工作正常,但似乎PHP方面没有得到正确的值,或者json编码不正确。
我有什么想法可以解决这个问题吗?
JSON:
{
"Email": [
{
"Name": "My Name",
"Phone": "1234567",
"Email": "my@mail.com",
"Interested_in": "Text text.",
"User_id": "1"
}
]
}
PHP:
$timeStamp = time();
$new_email = $_POST['NewMail'];
$email_info = $new_email->Email;
// New Email
mysql_query("INSERT INTO " . $dbPrefix . "touches (date, user_id, name, phone, email, interested_in, seen, status) VALUES ('".safeSQL($timeStamp)."', '".safeSQL($email_info->User_id)."', '".safeSQL($email_info->Name)."','".safeSQL($email_info->Phone)."', '".safeSQL($email_info->Email)."', '".safeSQL($email_info->Interested_in)."', '0', '1')") or die("Query failed: " . mysql_error());
更新
发送到PHP的Jquery
$( document ).on('click', '#send_touch', function(){
new_email = [];
new_email.push({
Name: $('#name').val(),
Phone: $('#phone').val(),
Email: $('#email').val(),
Interested_in: $('#interested_in').val(),
User_id: $('#email_user_id').val()
});
new_email = JSON.stringify({Email: new_email}, null, "\t");
$.ajax({
url: "core.php",
type: "post",
data: { NewMail: new_email
},
success: function(data){
},
error: function(){
}
});
});
还尝试将PHP更改为:
$timeStamp = time();
$new_email = json_decode($_POST['NewMail']);
$email_info = $new_email->Email[0];
// New Hashtag
mysql_query("INSERT INTO " . $dbPrefix . "touches (date, user_id, name, phone, email, interested_in, seen, status) VALUES ('".safeSQL($timeStamp)."', '".safeSQL($email_info->User_id)."', '".safeSQL($email_info->Name)."','".safeSQL($email_info->Phone)."', '".safeSQL($email_info->Email)."', '".safeSQL($email_info->Interested_in)."', '0', '1')") or die("Query failed: " . mysql_error());
答案 0 :(得分:2)
我相信你错过了php方面的解码:
$new_email = json_decode($_POST['NewMail']);