简体   繁体   English

如何使用jquery显示数据库中的图像

[英]how to display image from database using jquery

I have a problem with displaying an image using jquery.我在使用 jquery 显示图像时遇到问题。 The image is retrieved from the database, and i want to display it using jquery.该图像是从数据库中检索的,我想使用 jquery 显示它。 below is my codes下面是我的代码

index.php索引.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 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显示此处.html

<div id="imageContainer>
    <img src="img/...."> 
</div>

Try this尝试这个

success:function(data){
   $('#imageContainer').html('<img src="'+data+'"/>');
},

i hope this will help you我希望这能帮到您

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

index.php索引.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 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();

尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM