我完全有一个网站,想用PHP发送同一页面上的信息和数据时间我得到了典型的可能但是我希望它通过AJAX调用完成,但是使用Ajax整个页面的数据输出是再次显示,你请告知如何做到这一点,以及下面的例子,一个页面是我的------->只有一页
$(document).ready(function(){
$('#button').click(function(){
var id = $(this).attr('id_1');
$.ajax({
url:'new.php',
type: "post",
data:{id:id},
success: function(data){
$('#result').html(data);
}
});
});
});
<div id="result"></div>
<a href="#" id="button" id_1="20">test</a>
答案 0 :(得分:0)
最好使用单独的php文件来处理ajax调用。
但如果你仍然想使用同一页面,你可以在开头检查post变量而不输出其他内容
if (isset($_POST['id'])){
exit("your data");
}
答案 1 :(得分:0)
因此,当您向new.php发送请求时,它基本上会输出与您访问http://www.mysite.com/new.php
相同的内容您需要做的是处理此请求并仅输出您想要输出的内容。所以在new.php的顶部,你添加:
if(isset($_POST['id']) // Or some post variable from the request
{
/* Here you know you're handling the Ajax request */
print "Response Data";
exit; // Stop execution of script here so rest of page won't be loaded
}
答案 2 :(得分:0)
如果您希望在没有先前内容的情况下显示ajax结果,请使用
$('body').html(data);
而不是在div中输出结果