2

I have been given one php file with multiple functions that are echoed. On the client end, $.getJSON has no problem processing a JSON object if only echo is allowed and the other two are commented out. However if the others are uncommented than get JSON breaks, I'm still all of the objects echo in the response. For JSON to process, does every php function need to be in a separate file? Or can JSON parse the different arrays separately from one response string? i.e

   $.getJSON(samefilename,function(array 1) {
        --- process here
   }
   $.getJSON(samefilename,function(array 2) {

   }

Sorry if this a complete newbie question. I'm a bit confused on the process of parsing the response string. I would of thought it would be $_get['array1'] and $_get['array2']being echoed from php - yet if I have more than one echo in the php file my jquery breaks and shows blank in html.

1
  • The whole response must always be valid JSON. Commented Aug 26, 2012 at 11:36

1 Answer 1

3

Instead of:

<?php
echo $var1;
echo $var2;

do like this:

<?php
echo json_encode(Array(
  'var1' => $var1,
  'var2' => $var2
));

and in jQuery:

$.getJSON('file.php',function(response) {
   alert(response.var1);        
   alert(response.var2);
}
Sign up to request clarification or add additional context in comments.

Comments

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.