Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Retrieve & display image from database – using AJAX[POST] Re: Retrieve & display image from database – using AJAX[POST]

#128716
atandrastoh
Participant

Hi this is my short and simple solution:


php:(up.ph)

$link=mysql_connect("localhost", "user", "pass") or die (mysql_error());// server, user, pass
mysql_select_db("images",$link);
$id = $_REQUEST;
$image = mysql_query($sql="SELECT thumb FROM pictures where id = $id");
$image = mysql_fetch_assoc($image);
$image=$image;
echo base64_encode($image);
mysql_close();
?>

html + Javascript:


-html-
-head-
-title--/title-
-script src="jquery-1.9.0.js" type="text/javascript" charset="utf-8" --/script-
-/head-
-body-
-img id = "imm" src="" alt="Red dot" /-
-button onclick = "loadimage($('#picid').val())"-Click me!-/button--input type="text" id="picid" value="1" placeholder=""-
-script type="text/javascript"-

function loadimage(id) {
$.ajax({
async: false,
url: 'up.php',
type: 'POST',
dataType: 'xml/html/script/json/jsonp',
data: {
id: id
},
beforeSend: function(){
$('#imm').attr('src','ajax_loader.gif');
},
complete: function(data, xhr, textStatus) {
//full solution read and change data:image/jpg from db
$('#imm').attr('src','data:image/jpg;base64,' + data.responseText);
},
success: function(data, textStatus, xhr) {

},
error: function(xhr, textStatus, errorThrown) {}
});
}
-/script-
-/body-
-/html-

I think this good for you :)