0

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());
Pravin
  • 441
  • 2
  • 8
  • 26
  • Looks about right. You'll need to base64 encode the image data using: btoa() – rrowland Jun 05 '15 at 06:24
  • looks fine. Did u get any error while debugging. – Venu Jun 05 '15 at 06:28
  • have updated like this..Its just displaying the image tag – Pravin Jun 05 '15 at 06:44
  • `contentType: "image/png",` — you aren't POSTing an image **to** the server. Don't claim that you are. – Quentin Jun 05 '15 at 06:47
  • 1
    **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jun 05 '15 at 06:47
  • Poster updated the original post 30 minutes ago. He was originally not base64 encoding the image. – rrowland Jun 05 '15 at 07:15

3 Answers3

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.com/chart?cht=qr&chs=200x200&chl=http%3a%2f%2fwww.facebook.com" />');

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

or:

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

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

or the way I prefer:

$('#container').html(
    $('<img/>', {
        src: 'https://chart.googleapis.com/chart?cht=qr&chs=200x200&chl=http%3a%2f%2fwww.facebook.com',
        alt: ''
    })
);
Vishwas Soni
  • 467
  • 10
  • 16
1
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;
sarath
  • 698
  • 6
  • 20
0
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());
Pravin
  • 441
  • 2
  • 8
  • 26