-2

My PHP finds a bunch of urls for images. I want to be able to display them on an app. I'm using AJAX to retrieve the returned array. Then I'm trying to parse it and put it into a javaScript array. Then I want to display the first image from it's URL with my build image function. Currently no images are displayed. I'm not sure where my issue is, I don't think the array is being parsed and I'm not even sure if it's returning correctly in the php.

Below is my PHP. Using JSON ecode and trying to return that array of paths.

<?php
include("mysqlconnect.php");
$select_query = "SELECT `ImagesPath` FROM `offerstbl` ORDER by `ImagesId` DESC";
$sql = mysql_query($select_query) or die(mysql_error()); 
$data = array();
while($row = mysql_fetch_array($sql,MYSQL_BOTH)){
$data[] = $row['ImagesPath'];
}
echo json_encode( $data );
?>

This is a idea of what is echoed and hopefully returned.

["http://server/~name/folder/images/07-08-2014-1407418088.png","http://server/~name/folder/images/05-08-2014-1407252096.png"]

This is my script using AJAX to take that array and parse it into an javaScript array.

 function importJson(str) {
 if (str=="") {
 document.getElementById("content").innerHTML="";
 return;
 }
 if (window.XMLHttpRequest) {
 // code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 } else { // code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 xmlhttp.onreadystatechange=function() {
 if (xmlhttp.readyState==4 && xmlhttp.status == 200){
 alert("onreadystatechange");
 var images = JSON.parse(xmlhttp.responseText);
 for (var i = 0; i < images.length; i++) {
 buildImage(images[i]);
 }
 }
 }
 xmlhttp.open("GET","http://server/~name/folder/content.php");
 xmlhttp.send();
 buildImage=function(src) {
 var img = document.createElement('img');
 img.src = src;
 document.getElementById("content").appendChild(img);
 }
 }
 window.onload = importJson();
 </script>

The first image should be placed in the div contents.

<body>
<div id="content"></div>
</body>
Cœur
39k25 gold badges206 silver badges281 bronze badges
asked Aug 22, 2014 at 16:36
3
  • possible duplicate of JSON array javascript Commented Aug 22, 2014 at 16:38
  • The issue here, is trying to change PHP to a JSON and parse it correctly. I'm having difficulty i think with outputting the JSON message and receiving. Parsing maybe also but much more complex than that example. Commented Aug 22, 2014 at 16:47
  • I believe I'm sending or receiving the whole php script instead of just the array? Commented Aug 22, 2014 at 17:55

1 Answer 1

0

It looks mostly good. You're encoding the data with json_encode and parsing it in JS with JSON.parse. That's correct.

Take advantage of your Javascript debugging tools like Chrome's "Developer Tools". Check the network(Net) tab in order to 1. make sure the AJAX request is being sent by your browser, 2. The response is received, and 3. the response data is formatted the way you expect it to be.

If you're receiving the data and it's correct, then something is going on in your parsing of the responseText. Slap a "debugger;" line in there and play with the result.

Also, you should probably add an else to the if statement checking the response.

 if (xmlhttp.readyState==4 && xmlhttp.status == 200){
 ...
 } else {
 alert("Error");
 }

Otherwise your app would silently fail if the server returns an invalid response.

answered Aug 22, 2014 at 16:48

14 Comments

I added else error into the if statement underneath the for loop for creating the images and I get three error alerts. One for every image in the array I assume. I assume that means Im not parsing the array correctly.
Actually, I think that would suggest something wrong in the request-response cycle. Each alert would be for the different ready states. 0: request not initialized 1: server connection established 2: request received 3: processing request 4: request finished and response is ready
I believe it is. The "alert("onreadystatechange");" in my above code does become active after three error messages.
I also believe the second if statement in the importJson function, is working but not the first.
if (str=="") { document.getElementById("content").innerHTML=""; return; } That if statement you mean? You're not passing in an empty string so it's not going to pass (str == "").
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.