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.

3 Answers 3

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);

});
Sign up to request clarification or add additional context in comments.

3 Comments

i was gonna say embed them in an object but you raced me :D +1
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?
Oh never mind, I got it working it was just a little problem in another part of my jquery, thank you very much!
0

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

3 Comments

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 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().
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?
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.
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.

1 Comment

Thanks very much! Those documentations really helped out as well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.