0

How can I pass multiple variables between PHP files using jquery with json? Ex:

$.post("example.php", {asdf:asdf, sdfg:sdfg}, function(data){

        $('section').html(data);
});

But instead of just using the .html function I want to retrieve multiple variables from the PHP file and then use them for .html on my page. I can't just use data because that only outputs whatever PHP returns so to my understanding php can only return one thing in a form of an echo(), but how can I make it retrieve more than just that one variable? I think I have to use JSON but I have never used JSON before so I would appreciate it if someone could help me out. Thanks.

Stedy
  • 7,359
  • 14
  • 57
  • 77
randomphp
  • 233
  • 2
  • 4
  • 14

3 Answers3

1

You can return a JSON object from php ( Returning JSON from PHP to JavaScript? ). And then in your jquery you can acees it like this,

$.post("example.php", {asdf:asdf, sdfg:sdfg}, function(data){

 $('section1').html(data.Variable1);
 $('section2').html(data.Variable2);

});
Community
  • 1
  • 1
Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92
  • i was gonna say embed them in an object but you raced me :D +1 – Dany Khalife Nov 21 '11 at 03:22
  • Alright after I tried using this example I had my php file like this by the end: echo json_encode(array('page' => 'Working!', 'options' => 'yes', 'session' => 'active')); and my jquery was $('section1').html(data.page); $('section2').html(data.options); but that didn't work, any ideas on why it didn't work? – randomphp Nov 21 '11 at 03:39
  • Oh never mind, I got it working it was just a little problem in another part of my jquery, thank you very much! – randomphp Nov 21 '11 at 03:47
0

You could have the php script output javascript code creating your variables and then just eval() it.

Syntax Error
  • 4,475
  • 2
  • 22
  • 33
  • You could - however, eval() comes with significant performance hits, and generally can be avoided altogether. Some background on "eval is evil" can be found at http://www.jslint.com/lint.html. Particularly in the case of this specific question: there are well-understood and widely-accepted ways to do what's needed without eval(). – Christopher Nov 21 '11 at 03:25
  • I was worried this was a little stinky, js is not my forte. Is this bad enough that it should not even used as a "quick and dirty" solution? Should I remove? – Syntax Error Nov 21 '11 at 03:29
  • No; for quick-and-dirty, for environments where you control everything and can guarantee (or at least *assume* you can guarantee :) ) the cleanliness of the data, it can be okay. While not necessarily the One True Answer to this question, it's not always wrong. Putting aside security concerns, eval() always carries with it a hit, and instead of using programming constructs you fire up a whole new interpreter and effectively make debugging much more difficult. – Christopher Nov 21 '11 at 03:31
0

See the jQuery post() documentation here. Check out the dataType parameter, which tells jQuery what kind of data to expect back from the server.

See the json_encode() and json_decode() PHP functions here. Use json_encode() to prepare your populated data structure for echo()'ing back to the client.

Christopher
  • 764
  • 4
  • 6