Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tlug] Something's not right about my JSON



TLUG,

I've got some Javascript code that creates a JSON string out of a Javascript array, then passes it to a PHP script, which in turn makes it a PHP array.

Or, at least, I almost have that. I'm not getting the results I'm hoping for, and I think it's because I'm just a little confused about how I'm nesting my arrays.

The goal is to create a nested array that looks something like this:
{
loginData : {
   "email" : "some@example.com"
   "password" : a1oirngdf09unsvlsd90"
   }
"newUserData" : "No"
}

I hope I have the syntax for that right.

I've tried creating this JSON sting using the following code (edited a bit for brevity - hopefully no typos/syntax errors):

var loginData=new Array();
loginData.email=document.getElementById('email').value;
loginData.password=$.md5(document.getElementById('password').value);
userData=new Array();
userData.login=loginData;
userData.newUserData="No";
jsonString = JSON.stringify(userData);
$.post('jsonhandler.php', {JSON: jsonString}, checkResult, "json");

Then on the PHP side, I extract the data I want like so:

$array = json_decode(stripslashes($_POST['JSON']), true);
if (myFunctionThatChecksLoginAndReturnsTrueOrFalse($karray['userData']['email'],
$array['userData']['password']))

I think I'm close in the code, but the the array is not constructed properly at some stage.

For one thing, when I output the value of the stringified userData and
look at it in Firebug, it simply shows me:
[]

Can someone point out where I'm going wrong?

Any advice would be much appreciated.

--
 Dave M G


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links