dseve40868 2013-09-03 17:37
浏览 81
已采纳

将JSON数据发送到PHP并使用它

I have an issue with sending json data with ajax to a php file and then use it to upload to the server.

If I'm making echo of the variable $email_info->Name the returned data value is empty. I've tried json_decode but it doesn't do it.

This is the code I have,

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){  
                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;

}

If I make an echo on the PHP side for $_POST['NewMail'] I get returned this:

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

How can I fix this?

  • 写回答

3条回答 默认 最新

  • duanbi2760 2013-09-03 17:45
    关注

    Replace this part in PHP :

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

    by this :

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

    This should fix the issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致