0

I retrieve data from my database as such:

while($row = mysql_fetch_array($result))
{
    echo $row['uid'];
    echo $row['note'];
}

and I print it with ajax as such:

xmlhttp.onreadystatechange = function () {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          console.log(xmlhttp.responseText)
      }
   }

but the problem is that the uid and note its one string. For example:

"45 note goes here"

How can I print each row separately. Something like this:

xmlhttp.onreadystatechange = function () {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          console.log(  <?php echo json_encode($row['uid']); ?>  );
          console.log(  <?php echo json_encode($row['note']); ?>  );
      }
   }
4
  • You're very close: gather data with mysql_fetch_array into one array and out it in one echo as json with json_encode. Commented Mar 14, 2012 at 9:42
  • I know im really close. I tried many ways but I always get null. It should be something in my php. Can you please post an answer on how to because I pretty much tried everything I thought of Commented Mar 14, 2012 at 9:43
  • @jQuerybeast "I tried many ways but I always get null." Have you checked your MySQL result? Did you get something back from the database? Maybe your query is wrong or just returns an empty result for the requested parameters… Commented Mar 14, 2012 at 9:47
  • @feeela If I just do responseText then it works. So there is no problem with my MySQL Commented Mar 14, 2012 at 9:49

4 Answers 4

1

You're pretty much right. In your php script, return a json string, then in your javascript you can parse that string into a javascript object. So:

$json = array();
while($row = mysql_fetch_array($result))
{
   $json['uid']=$row['uid'];
   $json['note']=$row['note'];
}
print json_encode($json);

Then

xmlhttp.onreadystatechange = function () {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      var jsontext = xmlhttp.responseText;
      var json = JSON.parse(jsontext);
      console.log(json.uid)
      console.log(json.note)
  }
}
3
  • Its pretty much the idea I was looking for but unfortunately this doesnt work. This var json = JSON.parse(xmlhttp.responseText); gives me an error: Uncaught SyntaxError: Unexpected end of input. Because xmlhttp.responseText doesn't give any response when I use $json['uid']=$row['uid'];. So pretty much its all wrong? Commented Mar 14, 2012 at 9:54
  • When I use the example in my question, xmlhttp.responseText outputs all rows. Commented Mar 14, 2012 at 9:57
  • Yeah, sorry; it was somewhat hasty code that works with a single row (the last one if you're calling multiple rows). If you're returning a number of rows, you'd want the $json to be a multidimensional array (e.g. $json[$i]['uid'] etc and then increment $i every run through), and then loop through your json object etc. Commented Mar 14, 2012 at 10:30
0

Return $row as a json object.

while($row = mysql_fetch_array($result))
{
    $data[] = $row;
}
echo json_encode($data);

In your javascript you should now be able to use xmlhttp.responseText.0.uid (you probably will want to loop over the different rows).

Note: I have never done this with pure javascript, only with jQuery and do not know for sure if this will work as is. You might need to send an additional header that indicates the response is JSON (header('Content-type: application/json');), you might need to parse the responseText first so you can interpret it as an object.

2
  • This doesnt work because xmlhttp.responseText.0.uid is not valid. Commented Mar 14, 2012 at 9:56
  • As @Apemantus mentioned and I guessed, you still had to parse the responseText, but I see you've found your answer, good luck with the development! Commented Mar 14, 2012 at 12:13
0

You are trying to json_encode the individual keys in the array, try doing json_encode($row); else you will have a JSON array ['my user id'] rather then a JSON object {uid:'my user id'}

Also try using jQuery and you will be able to parse the response as a JSON object.

$.post('myurl', {mydata}, function (jsonArray){/*success function*/}, 'json');
-1

Any chance to use a JSON parser on client side (jQuery's one for example) and have on your JavaScript something like

for(i=0; i<rows.length; i++)
console.log(rows[i])

where rows is $.parseJSON(xmlhttp.responseText)

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.