Answer the question
In order to leave comments, you need to log in
How to load image from database via ajax using POST method?
Hello!
Can you please tell me how to load an image from the database into html code via ajax?
This is how the request is processed without problems:
$('#depIcon').html('<img src="php/getDepImage.php?id='+selectedID+'" alt="" style="width:70px; height:75px;">');
$.ajax({
url: 'php/getDepImage.php',
type: 'POST',
data: {param: selectedID},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log(XMLHttpRequest);
console.log(textStatus);
console.log(errorThrown);
},
dataType: "json",
success: function(data){
}
});
Answer the question
In order to leave comments, you need to log in
Factorization in integers is faster than existing analogues. Especially with GPU and FPGA
I look, and I just don’t understand: what does Ajax have to do with it?
Loading any image by the SRC attribute is an asynchronous request in itself. Why push into Ajax what is already asynchronous?
var event = 'click'; // hover, keydown ..
var getImage = function( selectedID ) { $('#depIcon').html('<img src="php/getDepImage.php?id='+selectedID+'" alt="" style="width:70px; height:75px;">'); }
$('#trigger').on(event, getImage( selectedID ));
You have an error in the code in:
instead of id you wrote param
Google in the direction of base64
In the application, you will need to select a unit from the list. After that, a number of indicators on the work of this unit, including a picture, are pulled from the database. You can select the next division and other indicators and another symbol will be pulled from the database. I have no idea how to do it through src
Thank you, I will try your code. But now I'm trying to present the request in the form:
$.ajax({
url: 'php/getDepImage.php',
type: 'POST',
data: {param: selectedID},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log(XMLHttpRequest);
console.log(textStatus);
console.log(errorThrown);
},
dataType: "json",
success: function(data){
}
});
Tried to do it with working code
Write an if condition, but could not determine the value of the incoming empty image. I looked at the value of the images coming into the div $('#depIcon').value, but what about the "empty" image, what about the "filled" image has this value undefined. There are no ideas other than to track the return of data in success: function(data){ } . Perhaps you know how to implement this check?
<img src="some.png" onerror="setDefaultSrc(e)">
<script>
setDefaultSrc(e){
$(e.target).attr({src:"default.png"});
}
</script>
In this form, the code really does not work. But the "onError" applied in it just saved. It was with its help that I was able to catch the "empty" images that came from the database. And then already register the insertion of a stub picture. It turned out the following:
Image request:
setDefaultSrc() function:
function setDefaultSrc(){
$('#depSymbol').html('<img src="images/somepicture.png">');
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question