2

I'm using this function to send json to a php page:

function update_records(data) {
    data = data;
    $.ajax({
        type: 'POST',
        cache: false,
        timeout: 2000,
        contentType: 'application/json',
        url: 'update.php',
        data: data, //'data='+data+'&aid=0',
        success: function() {
            success_message('success');
        },
        error: function(){
            failure_message('failure');
        }
    }); 

In firebug I can see the posted data:

[{
    "postid": 66,
    "values": [
        "field_key=a",
        "oldvalue=b",
        "newvalue=c dad"
    ]
}]

On my php page how can I $_REQUEST the object? Or am I doing it all wrong?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
lior r
  • 2,220
  • 7
  • 43
  • 80
  • Something is strange in your request: You set contentType to "application/json" but send the data as "application/x-www-form-urlencoded" – CodeZombie Feb 17 '12 at 12:15

5 Answers5

1

i actually did solve this one using this code on my js

this is how i collected my data i have created the object

jsonObj={"postid":postid,"value":value};
var jsonString=JSON.stringify(jsonObj);

now i sent the object using ajax

$.ajax({
  type: 'POST',
  url: siteurl+'/wp-content/themes/crm/modules/update_lead.php',
  dataType : 'json',
  data: {action:actionType,data:data},
});

and this on the target PHP file ( /wp-content/themes/crm/modules/update_lead.php)

$json=json_decode(stripslashes($_POST['data']), true);

hope this helps ...

lior r
  • 2,220
  • 7
  • 43
  • 80
0

You can use the json_decode function to decode your JSON string into an array.

Jules
  • 7,148
  • 6
  • 26
  • 50
  • problem is i get null when trying to $_REQUEST['data'] so json_decode is of course not working – lior r Feb 17 '12 at 10:37
  • Then something goes wrong in your posting. Also use `$_POST`. Then try adding this: `contentType: "application/json; charset=utf-8"`. – Jules Feb 17 '12 at 12:35
0

JQuery has a neat function that allows us to read external and local JSON files. jQuery.getJSON( url, [data], [callback] )

The first parameter of this function, the URL you are planning to read, is required. The second parameter is used if you need to POST data to the URL. Last but no least, the callback function, although not required, is almost always necessary.

Rupesh Pawar
  • 1,887
  • 12
  • 17
0

First of all you should pass the data as "application/json" and not as "application/x-www-form-urlencoded".

...,
data: JSON.stringify(data),
...,

On the server side use json-decode() to decode the JSON encoded string into an object. This functions is very strict and relies on properly written JSON.

It tried it with the JSON you provided and it worked perfectly.
Example: http://codepad.org/WOH2wGZv

I recommend to get rid of the surrounding square brackets if you pass only one object. I would also ensure the "values" are passed in JSON format instead of a string. This results in the following JSON:

{
   "postid":66,
   "values":{
      "field_key":"a",
      "oldvalue":"b",
      "newvalue":"c dad"
   }
}
CodeZombie
  • 5,367
  • 3
  • 30
  • 37
-1

On the PHP side, use $_POST['postid'] to get '66', etc. If you used type: 'GET' in your AJAX query, you should've used $_GET['postid'] on the PHP side.

Florian Margaine
  • 58,730
  • 15
  • 91
  • 116
  • the json object in my sample is with only 1 line in it i will have multiple id's in the future. i have tried using $_REQUEST['data'] but i get a NULL result. – lior r Feb 17 '12 at 10:36
  • That's because you won't get the javascript variable `data` in PHP. You only get the datas you send to it. Use `var_dump($_POST);` to see what exactly you're receiving on the PHP side. Also, don't use $_REQUEST : http://stackoverflow.com/a/2142754/851498 – Florian Margaine Feb 17 '12 at 10:41
  • var_dump($_POST); returns array(0) {} why not to use $_REQUEST ? – lior r Feb 17 '12 at 10:45
  • If var_dump returns array(0), then the problem is not in PHP. From what you're showing, I don't know where it is, though. See why not using $_REQUEST in the link I provided in my other comment :) – Florian Margaine Feb 17 '12 at 10:47