我完全有一个网站,想用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>
最好使用单独的 php 文件来处理 ajax 调用。
但是如果你仍然想使用相同的页面,你可以在开头检查 post 变量,而不输出其他内容
if (isset($_POST['id'])){
exit("your data");
}
因此,当您向new.php发送请求时,它的输出基本上与您访问过 http://www.mysite.com/new.php
相同您需要做的是处理此请求,并且仅输出您希望它输出的内容。因此,在新.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
}
如果您希望 ajax 结果在没有前面的内容的情况下显示,请使用
$('body').html(data);
而不是在div 中输出结果