0

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

     ?>

2 Answers 2

1

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.

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

8 Comments

it just returns "undefined: :/
@user2484648, Doubtful, since it says "object" in your question. Use your browser tools as I suggested to narrow the problem down.
@user2484648, You looked at the raw response data from your PHP script and it just says undefined? I doubt it.
No, i am using var id = data[0].id; alert(id);
@user2484648, How do you expect me, or anyone, to help you if you don't even follow the instructions I said for further debugging this problem? You need to narrow this down. I cannot see your server, and the output from your code. Show the raw output from your PHP script. You are wasting everyone's time.
|
0

I know this was back in June and has been helpful but something that really helped me to understand or at least navigate the JSON better was it's stringify method:

JSON.stringify(SOME_JSON_OBJECT);

As you can clearly see what you are passing in which becomes the object, this will dump it back out as a String of all of the JSON elements of the object.

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.