javascript - How to display the image from ajax success function? - Stack Overflow

I have created ajax function like this...In this I will get the value from run time and i need to retur

I have created ajax function like this...In this I will get the value from run time and i need to return the photo according to that value..In success function i need to display that image in particulat div

var num=document.getElementById('number').value;
$.ajax({
    url:"image.php?val="+num,
    contentType: "image/png",
    success:function(img)
    {
        $('#image').html('<img src="data:image/png;base64,' + img + '" />');
    }
    });

image.php page

$sql_sub = select_query("select pic from  photo  where picnum=".$_GET['val']."");
$img = $sql_sub[0][0]->load();
header("Content-type: image/png");
ob_start();
imagepng($img);
echo "data:image/png;base64,", base64_encode(ob_get_clean());

I have created ajax function like this...In this I will get the value from run time and i need to return the photo according to that value..In success function i need to display that image in particulat div

var num=document.getElementById('number').value;
$.ajax({
    url:"image.php?val="+num,
    contentType: "image/png",
    success:function(img)
    {
        $('#image').html('<img src="data:image/png;base64,' + img + '" />');
    }
    });

image.php page

$sql_sub = select_query("select pic from  photo  where picnum=".$_GET['val']."");
$img = $sql_sub[0][0]->load();
header("Content-type: image/png");
ob_start();
imagepng($img);
echo "data:image/png;base64,", base64_encode(ob_get_clean());
Share Improve this question edited Jun 5, 2015 at 6:43 Pravin asked Jun 5, 2015 at 6:19 PravinPravin 4412 gold badges9 silver badges26 bronze badges 6
  • Looks about right. You'll need to base64 encode the image data using: btoa() – rrowland Commented Jun 5, 2015 at 6:24
  • looks fine. Did u get any error while debugging. – Venu Commented Jun 5, 2015 at 6:28
  • have updated like this..Its just displaying the image tag – Pravin Commented Jun 5, 2015 at 6:44
  • contentType: "image/png", — you aren't POSTing an image to the server. Don't claim that you are. – Quentin Commented Jun 5, 2015 at 6:47
  • 1 Danger: You are vulnerable to SQL injection attacks that you need to defend yourself from. – Quentin Commented Jun 5, 2015 at 6:47
 |  Show 1 more ment

3 Answers 3

Reset to default 3

It looks perfect..You may have an issue in tag. Check first that tag. However .append works great.

Have you tried this:

$('body').append('<img src="https://chart.googleapis./chart?cht=qr&chs=200x200&chl=http%3a%2f%2fwww.facebook." />');

$('#div_where_you_will_sho_qr_code').append(data.toString());

or:

$('#container').html('<img src="https://chart.googleapis./chart?cht=qr&chs=200x200&chl=http%3a%2f%2fwww.facebook." />');

where #container is some DOM element to harbor your image.

or the way I prefer:

$('#container').html(
    $('<img/>', {
        src: 'https://chart.googleapis./chart?cht=qr&chs=200x200&chl=http%3a%2f%2fwww.facebook.',
        alt: ''
    })
);
var num=document.getElementById('number').value;
$.ajax({
    url:"image.php?val="+num,
     type: "POST",
     dataType: "html",
    success:function(data)
    {
        $('#image').html(data));
    }
    });

image.php

$sql_sub = select_query("select pic from  photo  where picnum=".$_POST'val']."");
$img    = $sql_sub[0][0]->load();
$image  = '<img src="data:image/png;base64,'.$img.'" />';
echo $img;
var num=document.getElementById('number').value;
$.ajax({
url:"image.php?val="+num,
 type: "POST",
 dataType: "html",
success:function(img)
{
    $('#image').html('<img src="data:image/png;base64,' + img  + '" />');
}
});

image.php

$sql_sub = select_query("select pic from photo where picnum=".$_GET['val']."");
$img = $sql_sub[0][0]->load();
header("Content-type: image/png");
ob_start();
echo $img;
echo "data:image/png;base64,", base64_encode(ob_get_clean());

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744308742a4567846.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信