Reputation: 93
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.
Upvotes: 2
Views: 101
Reputation: 16943
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);
}
Upvotes: 3