For example I have this ajax function but I can call only one json here what code to add to be able to get more than 1 json:
var blue_name = $("#blue_team #blue_name").val().trim();
var blue_type = $("#blue_team #blue_type").val().trim();
var red_name = $("#red_team #red_name").val().trim();
var red_type = $("#red_team #red_type").val().trim();
$.ajax({
url: "battle_review.php",
type: "post",
datatype: "json",
data: { bname: blue_name
btype: blue_type
rname: red_name
rtype: red_type},
success:function(data){
var toAppend = '';
if(typeof data === "object"){
for(var i=0;i<data.length;i++){
//append data here
}
$("#table1").append(toAppend);// first json
}
}
});
And here’s my php code and how I put data on my json I want is that i can get the two json’s $blue and $red How to do that?:
//get blue_team attributes
$blue = array();
$blue_result = $db->dataWarrior($battle,$name);
foreach($blue_result as $warrior){
$blue[] = $names;
}
//get red_team attributes
$red = array();
$red_result = $db->dataWarrior($battle,$name);
foreach($red_result as $warrior){
$red[] = $names;
}
echo json_encode(array('red'=>$red,'blue'=>$blue));
You want to retrieve more than one array on one single AJAX call? Just make… an array of array!
Instead of two
json_encodecall, just put:echo json_encode(array('red' => $red, 'blue' => $blue));Then, in your Javascript code, you can access them via
data.blueanddata.red.