0

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.keyIwantbut 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

9
  • 3
    when you console.log 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 json Commented Aug 26, 2015 at 9:33
  • @atmd i know this is my main issue if I had it as JSON I could access it but It is not return from JSON_encode as I expect it to and I am struggling to manipulate it to behave as I want it it. Commented Aug 26, 2015 at 9:35
  • 1
    I think the reserved word json_encode() is case sensitive. Try to change your JSON_encode() to small letters. Commented Aug 26, 2015 at 9:37
  • thanks @aldrin27 this has been changed output as follows ["Missed Entry","Overwrite"]["1","1"] . I just can not access it. Commented Aug 26, 2015 at 9:39
  • why not simply echo json_encode( array( 'members'=>$member, 'count'=>$count) ); Commented Aug 26, 2015 at 9:39

3 Answers 3

1

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?

Sign up to request clarification or add additional context in comments.

Comments

0

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

0

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

Comments

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.