title: how to retrieve images using json in php
body: <p>I want to retrieve images from json in php. Actually I want to get those images sources as json data through php and display using javascript. But I think m doing something wrong please help.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  function jsonGetImages(name){
  var thumbnailbox = document.getElementById("picturebox");
  var hr = new XMLHttpRequest();
  hr.open("POST", "jsonget.php", true);
  hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  hr.onreadystatechange = function() {
      if(hr.readyState == 4 &amp;&amp; hr.status == 200) {
          var d = JSON.parse(hr.responseText);
          picturebox.innerHTML = "";
          for(var o in d){
              if(d[o].src){
                  picturebox.innerHTML += '&lt;img src="'+d[o].src+'"&gt;';
              }
          }
      }
  }
  hr.send("name="+name);
  picturebox.innerHTML = "requesting...";
   }
   &lt;/script&gt;
   &lt;/head&gt;
   &lt;body&gt;
   &lt;div id="picturebox"&gt;&lt;/div&gt;
   &lt;script type="text/javascript"&gt;jsonGetImages('Jason');&lt;/script&gt; 
    &lt;/body&gt;
  </code></pre>
  
  <p>jsonget.php</p>
  
  <pre><code>&lt;?php
   header("Content-Type: application/json");
   $folder = 'images';
   $cn=mysql_connect("localhost","root","");
  if(!$cn){
      echo "&lt;b&gt;Connection couldn't be established.&lt;/b&gt;";
      die();
  }
  $db=mysql_select_db("test",$cn);
  if(!$db){
      echo "&lt;b&gt;Database doesn't exist.&lt;/b&gt;";
  }
  $dir = $folder."/"; 
  $dirHandle = opendir($dir);
  $name=$_POST['name'];
  $sql="SELECT * FROM users WHERE name='$name'";
  $result=mysql_query($sql);
  echo mysql_num_rows($result);
  $i=0;
  $jsonData = '{';
  while($row=mysql_fetch_array($result)){
  $name=$row['name'];
  $image_name=$row['image'];  
  $i++;
  $file=readdir($dirHandle);
      $src = "$dir$image_name";
  $jsonData .= '"img'.$i.'":{ "num":"'.$i.'","src":"'.$src.'", "name":"'.$name.'" },';
    }
   closedir($dirHandle);
   $jsonData = chop($jsonData, ",");
   $jsonData .= '}';
   echo $jsonData;
   ?&gt;
  </code></pre>
  
  <p>Please help. I can't find solution how to get images using json.</p>