php JQuery Ajax 成功:function()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2972545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:18:28  来源:igfitidea点击:

JQuery Ajax success: function()

phpjquery

提问by user351657

I post data to a php processing page like this:

我将数据发布到这样的 php 处理页面:

$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>?

我该怎么做才能成功:使用 function() 来显示消息<div id="result"></div>

I have tried:

我试过了:

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

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

该代码不会在 div 标签中显示消息。

How do I post data to the div?

如何将数据发布到div?

回答by gnarf

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

假设您的$.ajax()请求正在使用dataType: 'html'(或默认值,它会智能地猜测)您的success函数将接收返回的文本作为其第一个参数:

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

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

应该取出您mypage.php转储的任何 HTML ,并替换<div id="result">页面上的内容就好了!

I have assembled a jsfiddle demofor you to look at.

我已经组装了一个jsfiddle 演示供您查看。