Vignesh Anandakumar
Vignesh Anandakumar

Reputation: 167

How do I get two arrays in Ajax call?

JS CODE:

$.ajax({ 
         url: 'assignavailtrainers.php',
         data: {action:'test'},
         type: 'post',
         success: function(data) {

         }
 });

PHP CODE:

<?php
    $username = "trainerapp";
    $password = "password";
    $hostname = "localhost";
    $link = @mysql_connect($hostname, $username, $password);

    if(@mysql_select_db("trainer_registration"))
{

    $select_query_num = @mysql_query("select program_id,facilitator_id,availability_status from program_facilitator where availability_status in (1,2)");
    $select_query_name = @mysql_query("select facilitator_id,firstname,lastname,email_id from facilitator_details");
    $num_rows = @mysql_num_rows($select_query_num);
    $trainerdetails = [];
    $traineravaildetails = [];
    $i = 0;
    $j = 0;
    while($row = @mysql_fetch_assoc($select_query_num))
    {

        $trainerdetails[$i]['pgidi'] = $row['program_id'];
        $trainerdetails[$i]['facilitatorid'] = $row['facilitator_id'];
        $trainerdetails[$i]['avail_status'] = $row['availability_status'];
        $trainerdetails[$i]['idi'] = $row['facilitator_id'];

        $i++;
    }


        while($row1 =@mysql_fetch_assoc($select_query_name))
    {
        $traineravaildetails[$j]['facilitatorid'] = $row1['facilitator_id'];
        $traineravaildetails[$j]['firstname'] = $row1['firstname'];
        $traineravaildetails[$j]['lastname'] = $row1['lastname'];
        $traineravaildetails[$j]['emailidvalue'] = $row1['email_id'];
        $j++;
    }
    echo json_encode(array('result1'=>$trainerdetails,'result2'=>$traineravaildetails));
}
?>

Please help me with the code in the ajax success function area. I've tried using initChart2 but I get an error which says initChart2 is not defined. I don't seem to understand of how to get two arrays from PHP in ajax since I'm a newbie ajax. If someone can help me with the code along with explanation, it'd be great. And I also need to know how to differentiate outputs in ajax which are sent from PHP.

Upvotes: 1

Views: 1496

Answers (3)

bitifet
bitifet

Reputation: 3669

You have two choices:

First one is to simply parse received (text) data to JSON:

var jsonData = JSON.parse(data);
// or simply data = JSON.parse(data);

But the best one in my oppinion is to specify json dataType to the $.ajax() request:

$.ajax(
    data: {action:'test'},
    type: 'post',
    dataType: 'json',
    success: function(data) {
        ...
    }
});

This way, $.ajax() will also check for the validity of the received JSON data and error callback will be called instead of success one in case of wrong JSON data received.

...also is important to note you missed to send the json content-type header in your php with:

header("Content-Type: application/json");

Sending this header the dataType: 'json' parameter is no longer (strictly) necessary because $.ajax() guesses it by default attending to the received content-type. But, personally, I prefer to do both.

See $.ajax() documentation.

Upvotes: 1

PhuLuong
PhuLuong

Reputation: 537

To get ajax data:

$.ajax({ 
         url: 'assignavailtrainers.php',
         data: {action:'test'},
         type: 'post',
         success: function(data) {
           data.result1;
           data.result2;
         }
 });

You can use console.log(data); to view data structure

Upvotes: 0

Quentin
Quentin

Reputation: 943593

You forgot:

header("Content-Type: application/json");

… in your PHP.

While you are outputting JSON, you are telling the browser that it is HTML (which is the default for PHP) so jQuery isn't converting it to a useful data structure.

Add that and then you should be able to access data.result1 and data.result2.

Upvotes: 1

Related Questions