AoW
AoW

Reputation: 55

Jquery .load function response

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?

Upvotes: 0

Views: 492

Answers (3)

GreyRoofPigeon
GreyRoofPigeon

Reputation: 18133

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.

Upvotes: 2

Dexter
Dexter

Reputation: 173

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));

Upvotes: 0

Renzo Calla
Renzo Calla

Reputation: 7716

Use the callback function , documentation

jQuery('#divname').load('TEST.php',{
       'id' : id
  },  function() {
  alert( "Load was performed." );
});

Upvotes: 1

Related Questions