I am having trouble accessing the JSON data returned from my AJAX JQUERY call.
The AJAX executes correctly as does the query. I get the correct data returned; this consists of two arrays that I JSON_ENCODE. I need to be able to access both data sets independently.
It may make sense when you guys see the code:
**PHP**
$sql501 = "SELECT member,COUNT(member) as cont from loggederrors WHERE err = '".$hello."' GROUP BY member ";
$result50 =mysqli_query($con,$sql501);
$count50=mysqli_num_rows($result50);
$member = array();
$count = array();
while($row56 = mysqli_fetch_assoc($result50)) {
array_push($member, $row56['member']);
array_push($count, $row56['cont']);
}
echo JSON_encode($member);
echo JSON_encode($count);
?>
$member
and $count
are arrays and when they are returned and logged in my AJAX success function they look like this :
["Missed Entry"]["1"]["Missed Entry","Overwrite"]["1","1"]
The data is in the right order but seems to repeat which I understand is because I am pushing in each iteration to the arrays. Its the closest I got because the order is correct. I have tried building an associative array and using various different mysqli outputs i.e num_rows, fetch_assoc
Previously when I have used JSON I have never had an issue and have key to access the data:
Here is the AJAX:
$.ajax({url: 'getstaffresults.php',
data: {stafftosend:stafftosend },
type: 'post',
async: 'true',
success: function(data){
console.log(data);
}
});
I have previously been able to access individual keys with data.keyIwant
but this is not coming out as expected. Any help is much appreciated.
The goal is to get access to the two arrays. I do not need access to values in the arrays if that makes sense.
I have tried building a 2d array and encoding that but again I had not way of accessing it.
I have tried JSON.stringyfy , JSON.parse
3 Answers 3
Try packing the two responses into an array and only echo'ing one response.
$sql501 = "SELECT member,COUNT(member) as cont from loggederrors WHERE err = '".$hello."' GROUP BY member ";
$result50 =mysqli_query($con,$sql501);
$count50=mysqli_num_rows($result50);
$member = array();
$count = array();
while($row56 = mysqli_fetch_assoc($result50)) {
array_push($member, $row56['member']);
array_push($count, $row56['cont']);
}
$arrayResponse = array(
'member' => $member,
'count' => $count
);
echo JSON_encode($arrayResponse);
?>
I only suggest this because I am unsure if the javascript would parse to json strings side by side?
Comments
The nack it to create a single sensible PHP data structure and then convert it to json
You dont need 2 arrays for you information. Also if you echo 2 responces the likelyhood is the second will be lost in the ether.
So try this instead, create an array of arrays to return to the javascript code, which makes them easy to process here and in javascript.
$sql501 = "SELECT member,COUNT(member) as cont
from loggederrors
WHERE err = '".$hello."'
GROUP BY member ";
$result50 =mysqli_query($con,$sql501);
$count50=mysqli_num_rows($result50);
$member = array();
while($row56 = mysqli_fetch_assoc($result50)) {
$member[] = array('member' => $row56['member'],
'cont' => $row56['cont']);
}
echo JSON_encode($member);
?>
In fact you can simplfy this even further to
$sql501 = "SELECT member,COUNT(member) as cont
from loggederrors
WHERE err = '".$hello."'
GROUP BY member ";
$result50 =mysqli_query($con,$sql501);
$count50=mysqli_num_rows($result50);
$member = array();
while($row56 = mysqli_fetch_assoc($result50)) {
$member[] = $row56;
}
echo JSON_encode($member);
?>
Now change the javascript to add the dataType: 'json',
property
$.ajax({url: 'getstaffresults.php',
data: {stafftosend:stafftosend },
type: 'post',
async: 'true',
dataType: 'json',
success: function(data){
console.log(data);
}
});
and the data should be converted to javascipt array automatically
Comments
In your php:
echo json_encode(['member' => $member, 'count' => $count]);
In javascript:
$.ajax({url: 'getstaffresults.php',
data: {stafftosend:stafftosend },
type: 'post',
async: 'true',
success: function(data){
var parsedData = JSON.parse(data);
console.log(parsedData);
}
});
data
do you get["Missed Entry"]["1"]["Missed Entry","Overwrite"]["1","1"]
?? because that isnt json, just a string which you could parse as arrays, you'd be better changing your php to return valid jsonjson_encode()
is case sensitive. Try to change yourJSON_encode()
to small letters.["Missed Entry","Overwrite"]["1","1"]
. I just can not access it.echo json_encode( array( 'members'=>$member, 'count'=>$count) );