将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?

我有一个问题,即用ajax将json数据发送到php文件,然后用它上传到服务器。 p>

如果我正在回显变量 $ email_info-> Name code>,则返回的数据值为空。 我尝试过json_decode但它没有' 这是我的代码。 p>

Jquery: p>

  $  (文件).on('click','#send_touch',function(){
 
 new_email = []; 
 
 new_email.push({
 Name:$('#name')。val  (),
电话:$('#phone')。val(),
电子邮件:$('#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)\  n},
错误:function(){
} 
}); 
 
}); 
  code>  pre> 
 
 

PHP: p> \ n

  if($ _POST ['NewMail']){
 
 $ timeStamp = time(); 
 
 $ new_email  = json_decode($ _ POST ['NewMail'],true); 
 
 $ email_info = $ new_email->电子邮件[0]; 
 
 //新电子邮件
 mysql_query(“INSERT INTO”。  $ dbPrefix。  “touches(date,user_id,name,phone,email,interested_in,seen,status)VALUES('”。。gofeSQL($ timeStamp)。“','”。isfeSQL($ email_info-> User_id)。“','  “.safeSQL($ email_info-> Name)。”','“。safeSQL($ email_info-> Phone)。”','“。。gofeSQL($ email_info-> Email)。”','“。  safeSQL($ email_info-> Interested_in)。“','0','1')”)或死(“查询失败:”。mysql_error()); 
 
 echo $ email_info->名称; \  n 
} 
  code>  pre> 
 
 

如果我在PHP端为 $ _ POST ['NewMail'] code>做一个回音,我会得到以下信息: p>

  {
 \“Email \”:[
 {
 \“Name \”:\“John Doe \”,
 \“Phone \”:  \“1234567 \”,
 \“电子邮件\”:\“john@doe.com \”,
 \“Interested_in \”:\“文字在这里.. \”,
 \“User_id \”:\  “1 \”
} 
] 
} 
  code>  pre> 
 
 

我该如何解决这个问题? p> div>

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.

you need to specify to jquery that your sending data is a JSON type. Please configure the type of data in the ajax options. For example:

$.ajax({
       url: "core.php",
       type: "json", //here comes the data's type
       data: { NewMail: new_email
       },
       success: function(data){  
        alert(data)},
        error: function(){
        }`

I had magic_quotes turned 'on' on my server.

I realized it when testing Hossams code:

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

So finally this got to work with this code:

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']);

}

And I got the array with $email_info['Name'] instead of $email_info->Name