Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i am working on a little something, i have snipped together code from all over the place and am having trouble to get it to work.

So what im trying to do is have one page request another via ajax/json/php and query a database and then return the multiple results and print it out on the first page.

My problem is that whenever i run the client.php page, i only get back

id: [object Object] name: [object Object]

Instead of the data that i want, i know it is probably just a silly little mistake somewhere so any help would be appreciated.

And before i get grilled for using mysql instead of mysqli, its just how the code came that im using from http://openenergymonitor.org/emon/node/107

Client.php

    <html>
    <head>
         <script language="javascript" type="text/javascript" src="jquery.js"></script>
    </head>
    <body>

    <h2> Client example </h2>
    <h3>Output: </h3>
        <div id="output">this element will be accessed by jquery and this text    replaced</div>

    <script id="source" language="javascript" type="text/javascript">

    $(function () 
    {
    $.ajax({                                      
    url: 'api.php',                  //the script to call to get data          
    data: "",                        //you can insert url argumnets here to pass to   api.php
                                   //for example "id=5&parent=6"
    dataType: 'json',                //data format      

  success: function(data)          //on recieve of reply
    {

     var id = data[0];              //get id
     var vname = data[1];           //get name

     $('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname); //Set output element html

     }
     });
     }); 

     </script>
     </body>
     </html>

Api.php

    <?php 

     $host = "localhost";
     $user = "root";
     $pass = "";
     $databaseName = "abeiq_stock";
     $tableName = "variables";

     $con = mysql_connect($host,$user,$pass);
     $dbs = mysql_select_db($databaseName, $con);


     $query = "SELECT * FROM $tableName";
     $result = mysql_query($query);

     $rows = array();

     while($r = mysql_fetch_assoc($result)){
     $rows[] = $r; has the same effect, without the superfluous data attribute
     $rows[] = array('data' => $r);
      }

       echo json_encode($rows);

     ?>
share|improve this question

1 Answer

up vote 1 down vote accepted

You're sending back an entire array of objects. This JavaScript is completely wrong:

var id = data[0];              //get id
var vname = data[1];           //get name

It should be something like...

var id = data[0].data.id
var vname = data[0].data.id

Obviously, loop through instead of hard coding 0, but you get the idea.

You should also learn to do some basic debugging. If you use console.log(data) in your jQuery callback, you can see what is being deserialized. Also, you can use your browser tools to see the raw request/response data. If you learn to debug a bit, you could solve this quickly.

And, why are you adding the data to $rows twice? Don't do that.

share|improve this answer
it just returns "undefined: :/ – user2484648 Jun 14 at 5:01
@user2484648, Doubtful, since it says "object" in your question. Use your browser tools as I suggested to narrow the problem down. – Brad Jun 14 at 5:01
no matter what i try i get undefined back :/. – user2484648 Jun 14 at 5:03
@user2484648, You looked at the raw response data from your PHP script and it just says undefined? I doubt it. – Brad Jun 14 at 5:04
No, i am using var id = data[0].id; alert(id); – user2484648 Jun 14 at 5:06
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.