user351657
user351657

Reputation: 351

JQuery Ajax success: function()

I post data to a php processing page like this:

$insert = mysql_query(
    'INSERT INTO ' . $table . ' (' . substr($addfields,0,-1) . ') ' .
    'VALUES  (' . substr($addvals,0,-1) . ')');

I want to have:

if($insert): echo 'Message 1'; else: echo 'message2'; endif;

What do I do in my success: function() to display the message in <div id="result"></div> ?

I have tried:

success: function() {
     $(#result).html(html);
}

That code doesn't display the message in the div tag.

How do I post data to the div?

Upvotes: 3

Views: 38690

Answers (1)

gnarf
gnarf

Reputation: 106332

Assuming that your $.ajax() request is using dataType: 'html' (or the default, which will intelligently guess) your success function will receive the returned text as its first parameter:

$.ajax({
  url: '/mypage.php',
  dataType: 'html',
  success: function(data) {
    $('#result').html(data);
  }
});

Should take whatever HTML your mypage.php dumps out, and replace the contents of the <div id="result"> on the page just fine!

I have assembled a jsfiddle demo for you to look at.

Upvotes: 5

Related Questions