Sending JSON(jQuery) to PHP and decoding it

Posted by dscher on Stack Overflow See other posts from Stack Overflow or by dscher
Published on 2010-05-09T21:23:46Z Indexed on 2010/05/09 21:28 UTC
Read the original article Hit count: 229

Filed under:
|
|
|

I can't for the life of me figure out what I'm doing wrong. It seems like it should be simple because I can't find anyone else with this issue but I can't figure out to send basic data via javascript(jQuery) to PHP and decode it. For the sake of simplicity, this is what I have:

JAVASCRIPT

var json_data = { "name" : "john doe" };

$.ajax({
    type: "POST",
    url: "../bin/process.php",
    dataType: "json",
    data: json_data
    });

and my PHP FILE

$arr = json_decode("json_data", true);

$fp = fopen('data.txt', "w");
fwrite($fp, $arr['name']);
fclose($fp);

The file I'm writing ends up with nothing in it. If I do an:

fwrite($fp, 'test');

I get a file with the word test in it but no matter what I do I don't get the json data I sent.

Can someone please share a thorough example of A to Z. Thanks for any help.

© Stack Overflow or respective owner

Sending JSON(jQuery) to PHP and decoding it

Posted by dscher on Stack Overflow See other posts from Stack Overflow or by dscher
Published on 2010-05-09T21:42:03Z Indexed on 2010/05/09 21:58 UTC
Read the original article Hit count: 229

Filed under:
|
|
|

I can't for the life of me figure out what I'm doing wrong. It seems like it should be simple because I can't find anyone else with this issue but I can't figure out to send basic data via javascript(jQuery) to PHP and decode it. For the sake of simplicity, this is what I have:

JAVASCRIPT

var json_data = { "name" : "john doe" };

$.ajax({
    type: "POST",
    url: "../bin/process.php",
    dataType: "json",
    data: json_data
    });

and my PHP FILE

$arr = json_decode("json_data", true);

$fp = fopen('data.txt', "w");
fwrite($fp, $arr['name']);
fclose($fp);

The file I'm writing ends up with nothing in it. If I do an:

fwrite($fp, 'test');

I get a file with the word test in it but no matter what I do I don't get the json data I sent.

Can someone please share a thorough example of A to Z. Thanks for any help.

© Stack Overflow or respective owner

Related posts about JSON

Related posts about AJAX