0

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!

Philll_t
  • 4,267
  • 5
  • 45
  • 59

1 Answers1

1
$.each(obj, function (key, value) {
    data += "&obj[" + encodeURIComponent(key) + "]=" + encodeURIComponent(value);
});
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • Hi explosion Pills, I know I didn't mention this in my question, but what if there is an object within an object? Say for example: `obj={a:a, b:b, b:{c:c, d:d}}` Thanks! – Philll_t Feb 08 '13 at 02:32
  • 2
    In that case it would be much easier to just convert the object to a json string (`JSON.stringify`) and send it as a single parameter, then use `json_decode` and get your full nested object/array – Explosion Pills Feb 08 '13 at 03:05