Ajax Question

AJAX : Getting two different JSON variable from php using ajax

I am need to directly get the variable from the response.php which contains the needed value. How can I the filter that JSON variable separately? Or if i merge both array into 1 JSON call then how do i get the different value

AJAX

function getMaleRanking(){
var mr = 'maleRangking';
var mf = 'femaleRangking';

$.ajax({
url : 'ajax/ajax_drill.php',
type : 'post',
cache : false,
data :{ mr : mr , mf : mf , event_id : event_id},
success:function(data){
var rankings = data;
//get the first json
//get the sencod json

}
});
}
getMaleRanking();


My php

echo json_encode(resultMergeMale, JSON_NUMERIC_CHECK);
echo json_encode(resultMergeFem, JSON_NUMERIC_CHECK);

Answer

You have to give response in json format for fetching valued in multiple paramater. Your php code should be

$response=array('mr'=>'','mf'=>'');
if(isset($_POST['mr'])){
   foreach ($resultMergeMale as $key => $value) {
       $firstval =  $value;
       //get this first val
       $response['mr'] . =$firstval; 
   }

}

if(isset($_POST['mf'])){
    foreach ($resultMergeFem as $key => $value) {
       $secondval = $value
        //get this seaond val
       $response['mf'] .= $secondval ;
    }
}

echo json_encode($response);
die();

You have to pass dataType:'json', so Jquery automatically convert JSON string into JSON object and you can access by its property. Your Ajax code should be

function getMaleRanking(){
var mr = 'maleRangking';
var mf = 'femaleRangking';

$.ajax({
    url : 'ajax/ajax_drill.php',
    type : 'post',
    cache : false,
    dataType:'json',
    data :{ mr : mr , mf : mf , event_id : event_id},
    success:function(data){
        alert(data.mr);
        alert(data.mf);
    }
});
}
getMaleRanking();
Comments