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.