0

Suppose I am retrieving the data(in json form) from the first.php and second.php(coding in php). I want to assign this json data in the array of the jQuery array. let the array is var total = [index1,index2] the first json data coming from the first.php will assign to the first index of the total and the second json data coming from second.php assign to the second index of the total. how will I do this. I have tried the following code. I am the bigner so if there is any mistake then sorry. for replying me thank you.

jquery

var total = ['index1','index2'];
var ajax = new XMLHttpRequest();
var method = "GET";
var url = "first.php";
var asynchronous = true;

ajax.open(method,url,asynchronous);
    //sending ajax request.
ajax.send();

//receiving response from  the first.php
ajax.onreadystatechange = function(){
if(this.readyState== 4 && this.status == 200){ //readyState==4 means request is finish and response is ready 
    //status==200 is 'OK'
    var data = JSON.parse(this.responseText);
    for(var a=0; a<data.length; a++){
        total['index1']=data[a];
    }
  }
}console.log(total['index1']); //i want the output here

var url="second.php";
ajax.open(method,url,asynchronous);
ajax.send();
ajax.onreadystatechange = function(){
if(this.readyState== 4 && this.status == 200){ //readyState==4 means request is finish and response is ready 
    //status==200 is 'OK'
    var data1 = JSON.parse(this.responseText);
    for(var a=0; a<data1.length; a++){
        total['index2']=data1[a];
    }
  } 
}

first.php

include 'connection.php';

$data = array();
$query=mysqli_query($con,"select * from tabel_name ORDER BY id ASC");
if(mysqli_num_rows($query)){
    while($row = mysqli_fetch_assoc($query)){
        $data[] =$row; 
    };
    echo json_encode($data);
}

second.php

include 'connection.php';

$data = array();
$query=mysqli_query($con,"select * from tabel_name ORDER BY id ASC");
if(mysqli_num_rows($query)){
    while($row = mysqli_fetch_assoc($query)){
        $data[] =$row; 
    };
    echo json_encode($data);
}

I just wan to assign the first json array to index1 and show in console.log and second json array to the index2.

2 Answers 2

1

I found the problem. You use "this" in onreadystatechange callback, but "this" don't design your "ajax" variable.

I have renamed your "ajax" variable in "ajax1" and I have created a second "ajax2".

Please try this code :

var total = {index1: [], index2: []};
var ajax1 = new XMLHttpRequest();
var method = "GET";
var url = "first.php";
var asynchronous = true;

ajax1.open(method,url,asynchronous);

//receiving response from  the first.php
ajax1.onreadystatechange = function(){
    if(ajax1.readyState== 4 && ajax1.status == 200){ //readyState==4 means request is finish and response is ready
        //status==200 is 'OK'
        var data = JSON.parse(ajax1.responseText);
        for(var a=0; a<data.length; a++){
            total['index1'].push(data[a]);
        }
        console.log('=== index1 ===');
        console.log(total['index1']); //i want the output here
    }
}
//sending ajax request.
ajax1.send();


var ajax2 = new XMLHttpRequest();
var url="second.php";
ajax2.open(method,url,asynchronous);
ajax2.onreadystatechange = function(){
    if(ajax2.readyState== 4 && ajax2.status == 200){ //readyState==4 means request is finish and response is ready
        //status==200 is 'OK'
        var data1 = JSON.parse(ajax2.responseText);
        for(var a=0; a<data1.length; a++){
            total['index2'].push(data1[a]);
        }
        console.log('=== index2 ===');
        console.log(total['index2']); //i want the output here
    }
}
ajax2.send();
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for it. I have a Question in my mind? can i ask?
` total['index1']` in this the array will be there it means the first index of the total? i m right its equal to total['index1'] means total={index:[]}?? I am right
total['index1'] = total.index1 You can created it by var total = {index1: []} However total['index1'] is not total={index:[]} because "1" is missing. Note that "index1" is your initial key and we could have another key like "myindex" : total['myindex'] = total.myindex or total={myindex:[]}
Yeahh yeah sorry i missed "1"
0

You use a wrong property "lenght". A correct property is "length" in your "for" statements.

ajax.onreadystatechange = function(){
if(this.readyState== 4 && this.status == 200){ //readyState==4 means request is finish and response is ready 
    //status==200 is 'OK'
    var data = JSON.parse(this.responseText);
    for(var a=0; a<data.length; a++){
        total['index1']=data[a];
    }

    // Here, server has returned response
    console.log(total['index1']); //i want the output here
  }
}
// Here, server has not yet returned response

8 Comments

sorry i editted my question now you can see the right one
Ok, your console.log is run immediatly but ajax request is not already returned. Try insert console.log(total); just after your for statement in onreadystatechange callback.
my code is right or wrong. is the json data will correctly assigning to the indexs? you can edit my question and post it. as a answer
I think he's right, but you are in asynchronous mode. So, your server answers after you have ran console.log(total['index1']).
actually i am not getting you properly can you elaborate it for me?
|

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.