Question

I have a quick question here. I would like to pass an object through the uri.

So here is a set of data I am using already something like this:

a = 'a';
b = 'b';

obj = {
 d : 'd',
 c : 'c'
}

//so now URLencode
var data = '?a=' + URLEncode(a);
var data += '&b=' + URLEncode(b);
var data += '&obj=' + $.param(obj);

loadPage(url, data);

So when I load this in my PHP I would like to be able to access the obj as an array and something like this:

<?php
...
$_obj =  $this->_request->getParam('obj');

 echo $_obj[d];
 echo $_obj[c];

output of course would be this:

d
c

So, in conclusion how would you go about and do this so that my php works as shown above? I really appreciate your help and thank you in advance!

Was it helpful?

Solution

$.each(obj, function (key, value) {
    data += "&obj[" + encodeURIComponent(key) + "]=" + encodeURIComponent(value);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top