I'm trying to display response of PHP script within a div element <div id="divname"></div>
jQuery('#divname').load('TEST.php',{
'id' : id
});
My code works as expected and successfully insert «1» inside this div.
TEST.php file:
<?php
echo '1';
?>
...but I would also like to alert the response in the same time using jQuery, someting like this:
jQuery('#divname').load('TEST.php',{
'id' : id
},
alert(response); //????
);
Can You help me?
Use the callback function , documentation
jQuery('#divname').load('TEST.php',{
'id' : id
}, function() {
alert( "Load was performed." );
});
As already commented, I would use $.ajax
for this action:
$.ajax({
url: "TEST.php"
})
.done(function( data) {
// alert( "Returned data: " + data );
$('#divname').html(data);
})
.fail(function() {
alert( "Loading failed" );
});
You can then check if the action succeeded (done) or failed (fail).
In the done function, data is the data that returns from the request.
Check out this post about how callbacks work. The code below should work as you're expecting.
jQuery('#divname').load('TEST.php',{
'id' : id
}, response => alert(response));