开发者

JQuery Ajax success: function()

开发者_Python百科

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜