0

我在将带有 ajax 的 json 数据发送到 php 文件然后使用它上传到服务器时遇到问题。

如果我正在对变量进行回显,$email_info->Name则返回的数据值为空。我试过 json_decode 但它没有这样做。

这是我的代码,

查询:

$( 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){  
                alert(data)          
            },
            error: function(){
            }   
     });    

});

PHP:

if ($_POST['NewMail']) {

 $timeStamp = time();

 $new_email = json_decode($_POST['NewMail'], true);

 $email_info = $new_email->Email[0];

 // 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());

 echo $email_info->Name;

}

如果我在 PHP 端做一个回显,$_POST['NewMail']我会得到这个:

{
\"Email\": [
    {
        \"Name\": \"John Doe\",
        \"Phone\": \"1234567\",
        \"Email\": \"john@doe.com\",
        \"Interested_in\": \"Text here..\",
        \"User_id\": \"1\"
    }
]
}

我怎样才能解决这个问题?

4

3 回答 3

1

在 PHP 中替换这部分:

$new_email = json_decode($_POST['NewMail'], true);

这样 :

if (get_magic_quotes_gpc()) {
    $_POST['NewMail'] = stripslashes($_POST['NewMail']);
}
$new_email = json_decode($_POST['NewMail'], true);

这应该可以解决问题。

于 2013-09-03T17:45:01.303 回答
1

我在我的服务器上打开了magic_quotes。

我在测试 Hossams 代码时意识到了这一点:

if (get_magic_quotes_gpc()) {
    $_POST['NewMail'] = stripslashes($_POST['NewMail']);
}

所以最后这与这段代码一起工作:

if ($_POST['NewMail']) {

$timeStamp = time();

if (get_magic_quotes_gpc()) {
    $_POST['NewMail'] = stripslashes($_POST['NewMail']);
}

$new_email = json_decode($_POST['NewMail'], true);

$email_info = $new_email['Email'][0];

// 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());

var_dump($email_info['Name']);

}

我得到了数组$email_info['Name']而不是$email_info->Name

于 2013-09-03T18:42:13.033 回答
0

您需要向 jquery 指定您的发送数据是 JSON 类型。请在 ajax 选项中配置数据类型。例如:

$.ajax({
       url: "core.php",
       type: "json", //here comes the data's type
       data: { NewMail: new_email
       },
       success: function(data){  
        alert(data)},
        error: function(){
        }`
于 2013-09-03T17:46:24.090 回答