Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 1.22 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. My json array is echoing directly to the page for some reason
  2. theUrl = 'myphpquerypage.php?id=somenumber'
  3.  
  4. $.getJSON(theUrl, function(data) {        
  5.     var content = "";
  6.     $.each(data, function(index, array) {
  7.         content += "<div>" + array.filename + "</div>";
  8.         $(content).appendTo('#myContentDivTag');
  9.        
  10. $pdo = new PDO($dsn, $username, $password);
  11.     $rows = array();
  12. if(isset($_GET['id'])) {
  13.     $stmt = $pdo->prepare("call getimages(?)");
  14.     $stmt->execute(array($_GET['id']));
  15.     $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  16. }
  17. echo json_encode($rows);
  18.        
  19. [{"filename":"img4dc458200caa7.jpg"},{"filename":"img41b65171bd651.jpg"},    {"filename":"img41be6c0eae1a4.jpg"},{"filename":"img41e5d5eede9da.jpg"},    {"filename":"img41e5d64383039.jpg"},{"filename":"img422fb82b3768a.jpg"},    {"filename":"img422fc528cc8d1.jpg"}]
  20.        
  21. $.ajax("myphpquerypage.php",
  22. {
  23.     dataType: "json",
  24.     type: "get" /*can also be post*/,
  25.     data: {id: "somenumber"},
  26.     success: function(data)
  27.     {
  28.         var content = "";
  29.         for (var key in data)
  30.         {
  31.             content += "<div>" + data[key].filename + "</div>";
  32.         }
  33.  
  34.         $("#test").append(content);
  35.     }
  36. });
  37.        
  38. before div
  39.  
  40. <div id="test">
  41. </div>
  42.  
  43. after div