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
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) );