0

I am using the following code :

$(document).ready(function() {
        $.ajax({
            url: "tiles/chartValue.php",
            datatype: 'json',
            type: 'POST',
            success: function(data){                
                $.each(data, function(key, value) { 
                  alert(key + ': ' + value); 
                });
                       });
                });

Here is chartValue.php

<?php
    include("../functions.php");
    $DAO=new DBUtil();
    $DAO->initDB("../db/connection.properties");
    $k=$DAO->getChart();
    echo json_encode($k);
?>

here is the getChart function of functions.php

public function getChart()
{
    $sql = "SELECT a.total, round( (b.active *100) / total ) active_user, round( (c.inactive *100) / total ) inactive_user
        FROM 
        (               
            SELECT IFNULL( count( * ) , 0 ) total FROM ashekan_info
        )a, 
        (               
            SELECT IFNULL( count( * ) , 0 ) active FROM ashekan_info WHERE is_active =1
        )b, 
        (               
            SELECT IFNULL( count( * ) , 0 ) inactive FROM ashekan_info  WHERE is_active =0
        )c";

        $result=mysql_query($sql,$this->connect()) or die(mysql_error());   
        $row = mysql_fetch_assoc($result);  
        $json = array('total'       =>$row['total'],
                  'active_user' =>$row['active_user'],
                  'inactive_user'   =>$row['inactive_user']
                 ); 
        return $json;

}

I get the result in firebug is {"total":"1","active_user":"0","inactive_user":"100"}

problem is it shows each character in alert box. i.e : {, ", t and so on.

how can i get 1, 0, 100 ? thanks in advance.

1
  • try alert(typeof(data)); ... is it object ? Commented May 13, 2011 at 14:07

3 Answers 3

7

Just a guess...

You have datatype: 'json', but JS is case sensitive, and the correct parameter is dataType. So it's ignoring that, taking text, and your each call is iterating through characters of a string.

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

Comments

0

Because the answer you get from your AJAX call is still a string. Parse it first with JQuery.ParseJson, then iterate the parsed variable.

1 Comment

Didn't occur to me, since the content type header isn't visibly set in the snippet either.
0

what I think you need is :

header("Content-type: application/json");

right before

echo json_encode($k);

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.