View Full Version : AJAX: Send JSON to PHP code from Javascript

02-22-2011, 04:37 PM
I'm new to CodingForums.com, so hello everyone!

I have some experience in javascript and php, but I am new to AJAX.
Here is the issue:

I have created a JSON object in javascript. I would like to send that object to a php file, have the php code process the JSON as an array, then send back a JSON object for the javascript to process.

After many hours of frustrated attempts, I ended up just copying a code from tutorial I found at http://www.prodevtips.com/2008/08/15/jquery-json-with-php-json_encode-and-json_decode/

Still, nothing.

Here is my code copied from the tutorial:

// Javascript file
<SCRIPT src="../../includes/jquery.min.js"></script>
<SCRIPT src="../../includes/jquery.json.min.js"></SCRIPT>
var data = new Object();
data.hello = "Hello";
data.world = 'World';
data.worked = " it worked ";
data.somebool = true;
data.array = new Array("he\"ll\"o", '"World"');
var dataString = $.toJSON(data);
$.post('phpfile.php', {data: dataString}, function(res){
var obj = $.evalJSON(res);
if(obj.somebool === true)
$("#result").html(obj.hello + ' ' + obj.array[1] + obj.worked + ". Message from PHP: "+obj.php_message);
<div id="result">This is not the text I am hoping to see...</div>

// phpfile.php

$res = json_decode($_REQUEST['data'], true);
$res["php_message"] = "I am PHP";
echo json_encode($res);

I am about to give up, and just convert my JSON to a string. But the ability to send JSON objects back and forth through AJAX seems like such a powerful tool, especially since I'm doing a lot of work with SQL databases.

I am using php v 5.2.16. I read there might be some compatibility issues with json_encode(), but only before php 5.2.....

I appreciate the help!

02-22-2011, 04:55 PM
I am about to give up, and just convert my JSON to a string.

I'm not sure what you mean by that. Your JSON string is a string.

Anyway, can you show this on a live site? There are so many things that could potentially fail, so it's hard to tell you anything without seeing it in action.

02-22-2011, 06:12 PM
Sorry, what I meant was that I could turn the data object into a string separated by ",". For example:

dataArray = new Array();
for (var prop in data) {
dataString = dataArray.join(",")

Then in the php code, separate the dataString back into an array, and work with it like that. This doesn't allow me to send the same complexity of information to the php code, but it's the compromise I'm working with for now...

BTW, I'm having no trouble receiving JSON objects back from the php.

You can see a live version of the unsuccessful code here: here (http://dylmg.earthcreaturespottery.com/sandbox/AJAX/jsCaller.html)

02-22-2011, 07:46 PM
The problem is definitely in the PHP. The Javascript component sends and receives ok, but PHP isn't sending back the extended object, but a new one only containing "php_message".

Your PHP code looks ok though, so could you please do a var_dump on $_REQUEST (it would be better to use $_POST, btw), on $res (before and after adding the additional index), and on json_encode($res)? At one of those steps, something is failing.

02-22-2011, 09:28 PM
I took a look at the php code, did a little more research, and I think I figured it out.
Apparently magic quotes are set on my server. The solution was just to add a stripslashes() tag.

Here is my working php code:

$res = json_decode(stripslashes($_POST['data']), true);
$res[php_message] = "I am PHP";

echo json_encode($res);

Thanks for the help!