0

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?

3
  • 1
    Use $.get with a callback or $.ajax. Commented Sep 15, 2017 at 12:57
  • I would also suggest to use $.get or $.ajax, than you can also check if the action executed, failed, succeeded, and more. See the documentation: api.jquery.com/jquery.ajax Commented Sep 15, 2017 at 13:00
  • 1
    Possible duplicate of Using jQuery load with promises Commented Sep 15, 2017 at 13:01

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You are missing $('#divname').html(data); to match the example code fully.
Thanks @Adder, changed it
1

Use the callback function , documentation

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.