1

I have a problem with displaying an image using jquery. The image is retrieved from the database, and i want to display it using jquery. below is my codes

index.php

<?php 
require_once("db.php");

if(isset($_POST['input'])):

$id = $_POST['id'];

    $selectImage = mysqli_prepare($link,"SELECT product_image FROM products WHERE id = ? ");
    mysqli_stmt_bind_param($selectImage, "i",$id);
    mysqli_stmt_execute($selectImage);
    mysqli_stmt_bind_result($selectImage,$image);
    mysqli_stmt_fetch($selectImage);

    if($image):
      echo $image;

    else:
       echo "no image to display";
    endif;

endif;
?>

ajax.php

function imgViews(){
    $(".imageView").click(function(){
        var id = $(this).attr("id");

        $.ajax({
                url: 'class/display_image.php',
                type: 'POST',
                data: {input: 'input',
                       id:id
                   },
                success:function(data){
                 ****display image*****
                },
                dataType: "text"
        });
    });
}
imgViews();

displayHere.html

<div id="imageContainer>
    <img src="img/...."> 
</div>
4
  • just send entire image tag with src from server, and .html() to the div. Commented Feb 13, 2019 at 7:16
  • @DevsiOdedra, please share your answer as an answer instead of a comment. Commented Feb 13, 2019 at 7:34
  • @dmh given answers are also do the same thing so OP can use it. Commented Feb 13, 2019 at 7:40
  • Sorry @DevsiOdedra, I couldn't see that when I added my comment (from triage). Commented Feb 13, 2019 at 17:28

4 Answers 4

1

Try this

success:function(data){
   $('#imageContainer').html('<img src="'+data+'"/>');
},
Sign up to request clarification or add additional context in comments.

Comments

1

i hope this will help you

$("#imageContainer img").attr("src", data);

Comments

0

index.php

<?php 
require_once("db.php");
if(isset($_POST['input'])):
$id = $_POST['id'];
$selectImage = mysqli_prepare($link,"SELECT product_image FROM products WHERE id = ? 
");
mysqli_stmt_bind_param($selectImage, "i",$id);
mysqli_stmt_execute($selectImage);
mysqli_stmt_bind_result($selectImage,$image);
mysqli_stmt_fetch($selectImage);

if($image):?>
<div id="img_id"><?php echo $image; ?></div> 
<?php  
else: ?>
<div id="img_id"></div>
<?php 
endif;
endif;
?>

ajax.php

function imgViews(){
    $(".imageView").click(function(){
        var id = $('img_id').text().trim();

        if(id != ""){
             $.ajax({
                    url: 'class/display_image.php',
                    type: 'POST',
                    data: {input: 'input',
                           id:id
                       },
                    success:function(data){
                     $('#imageContainer').html('<img src="'+data+'"/>');
                    },
                    dataType: "text"
            });
        }
    });
  }
  imgViews();

3 Comments

So the returned data in your answer will be <div id="img_id"></div> then how can you use .html('<img src="'+data+'"/>');
if your class/display_image.php is index.php then php code will remain same as yours
@Harrybaldaniya thanx for a trick. i modify the codes that made the returned data to be <img src='img/<?php echo $image;?>'>
0

Try This

$('#pic').html('"<img src="'+ picURL + data +'"/>');

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.