1

I am trying to make an ajax call to a php script that outputs images from a directory.

I have this as my php script:

<?php

 $con = @mysqli_connect('myhost','myusername','myuserpass','myuserDB') or die('Could not connect to the database.'." ". __FILE__ ." ". __LINE__);

 $photoFile = mysqli_query($con, 'select photoFile from photoInfo');
  while ($p = mysqli_fetch_array($photoFile)){
    echo '<img class="image" src='."../img/".$p['photoFile'].".jpeg />";
  };
 mysqli_close($con);

?>

And I'm trying to call it like this.

$(document).ready(function() {

   $.ajax({
       url: 'php/getImages.php',
       method: 'get',
       dataType: 'html'
           }).done(function(data){
          $('#imageBox').appendData(data);
     });
  });

What all am I doing wrong? When I run the php script on its own, it brings up the images but when I try to use ajax to call it, nothing happens.

2
  • Check with .html(data). Commented Dec 2, 2013 at 5:18
  • It's seems to be nothing wrong with code. Just try to put complete path in image src and try. Commented Dec 2, 2013 at 5:35

4 Answers 4

1

Can you try this, use $('#imageBox').html(data); instead of $('#imageBox').appendData(data);

<script type="text/javascript">
    $(document).ready(function() {

            $.ajax({
                url: 'php/getImages.php',            
                method: 'get',
                dataType: 'html'
            }).done(function(data){
                $('#imageBox').html(data);
            });
        });

</script>


  <div id="imageBox"></div>

In getImages.php,

    <?php

    $con = @mysqli_connect('myhost','myusername','myuserpass','myuserDB') or die('Could not connect to the database.'." ". __FILE__ ." ". __LINE__);

    $photoFile = mysqli_query($con, 'select photoFile from photoInfo');
    while ($p = mysqli_fetch_array($photoFile)){
        echo '<img class="image" src="../img/'.$p['photoFile'].'.jpeg" />';
    };
    mysqli_close($con);

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

1 Comment

After cleaning up my php script (Thanks to you and Harish!) switching to .html worked. Thank you!
0

Try this code:

  while ($p = mysqli_fetch_array($photoFile)){
           $img_arr[]= '<img class="image" src='."../img/".$p['photoFile'].".jpeg />";
  };
echo json_encode($img_arr);
exit;

In javascript

$.get('php/getImages.php',function(data)
  $.each(data,function(k,e){
     html+=e;

});
$('#imageBox').appendData(html);
},'json');

Comments

0
$photoFile = mysqli_query($con, 'select photoFile from photoInfo');
while ($p = mysqli_fetch_array($photoFile)){
    $imgpath = "../img/".$p['photoFile'].".jpeg";
    echo '<img class="image" src="'.$imgpath.'" />';
};
mysqli_close($con);

Comments

0

put the breakpoint in data that in coming from server [your php script] and check it, it coming in right format or not.

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.