Php 我如何不断刷新一个单独的div来保存帖子的id?
我正在用PHP、AJAX、MySQL等做一个博客。和往常一样,每个帖子都有自己的ID,在帖子里面你可以看到评论 我试图做的是通过使用AJAX调用Php 我如何不断刷新一个单独的div来保存帖子的id?,php,ajax,Php,Ajax,我正在用PHP、AJAX、MySQL等做一个博客。和往常一样,每个帖子都有自己的ID,在帖子里面你可以看到评论 我试图做的是通过使用AJAX调用comments.php文档并使用$('#comments').html(数据)将其放入div来刷新注释的div 每5秒执行一次,以实时维护评论,但问题是当div进行第一次刷新时,div会丢失帖子的ID,并说它是未定义的 如何刷新任何div,而不丢失帖子的ID 如果我使用post文件中典型的include(file.php)调用comments.php文
comments.php
文档并使用$('#comments').html(数据)将其放入div
来刷新注释的div
代码>
每5秒执行一次,以实时维护评论,但问题是当div
进行第一次刷新时,div
会丢失帖子的ID,并说它是未定义的
如何刷新任何div
,而不丢失帖子的ID
如果我使用post文件中典型的include(file.php)
调用comments.php
文件,我没有问题,但是使用这种方式我无法刷新内容
代码如下:
post.php:
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: 'support/comments.php',
success: function(data) {
$('#comments').html(data);
}
});
});
</script>
刷新div的脚本
:
<script language="Javascript" type="text/javascript">
function refreshDivs(divid, secs, url) {
// define our vars
var divid,secs,url,fetch_unix_timestamp;
// The XMLHttpRequest object
var xmlHttp;
try {
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
} catch (e) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
} catch (e) {
try {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("your browser doesn't support ajax.");
return false;
}
}
}
// Timestamp para evitar que se cachee el array GET
fetch_unix_timestamp = function () {
return parseInt(new Date().getTime().toString().substring(0, 10))
}
var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp;
// the ajax call
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
document.getElementById(divid).innerHTML=xmlHttp.responseText;
setTimeout(function(){refreshDivs(divid,secs,url);},secs*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}
window.onload = function startrefresh () {
//update content on real time
refreshDivs('comments',10,'support/comments.php');
}
</script>
函数refreshDivs(divid、secs、url){
//定义我们的变量
var divid、secs、url、fetch\u unix\u时间戳;
//XMLHttpRequest对象
var-xmlHttp;
试一试{
xmlHttp=newXMLHttpRequest();//Firefox、Opera 8.0+、Safari
}捕获(e){
试一试{
xmlHttp=newActiveXObject(“Msxml2.xmlHttp”);//Internet Explorer
}捕获(e){
试一试{
xmlHttp=新的ActiveXObject(“Microsoft.xmlHttp”);
}捕获(e){
警报(“您的浏览器不支持ajax。”);
返回false;
}
}
}
//时间戳para evitar que se cache el array GET
fetch\u unix\u timestamp=函数(){
返回parseInt(新日期().getTime().toString().substring(0,10))
}
var timestamp=fetch_unix_timestamp();
var nocacheurl=url+“?t=“+时间戳;
//ajax调用
xmlHttp.onreadystatechange=函数(){
if(xmlHttp.readyState==4&&xmlHttp.status==200){
document.getElementById(divid).innerHTML=xmlHttp.responseText;
setTimeout(函数(){refreshDivs(divid,secs,url);},secs*1000);
}
}
open(“GET”,nocacheurl,true);
xmlHttp.send(空);
}
window.onload=函数startrefresh(){
//实时更新内容
refreshDivs('comments',10,'support/comments.php');
}
您可以在URL中传递帖子id,如下所示:
url: 'support/comments.php?id=<?= $post_id ?>'
window.setInterval(function(){
$.ajax({
url: 'support/comments.php?id=<?= $post_id ?>',
success: function(data) {
$('#comments').html(data);
}
});
}, 5000);
url:'support/comments.php?id='
然后用setTimeout包装调用,如下所示:
url: 'support/comments.php?id=<?= $post_id ?>'
window.setInterval(function(){
$.ajax({
url: 'support/comments.php?id=<?= $post_id ?>',
success: function(data) {
$('#comments').html(data);
}
});
}, 5000);
window.setInterval(函数(){
$.ajax({
url:'support/comments.php?id=',
成功:函数(数据){
$('#comments').html(数据);
}
});
}, 5000);
并丢弃refreshDiv
这是假设comments.php检索注释,其他一些代码发布它们。好的,伙计们,我解决了这个问题。。。我会把代码留在这里,以防有人也有同样的问题。。。我所做的是构建一个隐藏的输入,并将此输入使用其值,如帖子的id,然后我将此输入的值发送到带有#('div').val的脚本,最后我将该值发送到comments.php文件,一旦到达。。。。我使用查询语句中的值进行比较,最终可以在正确的帖子中获得评论
这是密码
<script>
$(document).ready(function() {
window.setInterval(function(){
//Comentarios
var id = $("#idcomment").val();
$.get("support/comments.php", { idpost: id }, function(LoadPage){
$("#comment").html(LoadPage);
});
}, 5000);
});
</script>
$(文档).ready(函数(){
setInterval(函数(){
//科门塔里奥斯
var id=$(“#idcomment”).val();
$.get(“support/comments.php”,{idpost:id},函数(LoadPage){
$(“#注释”).html(加载页);
});
}, 5000);
});
什么是refreshDivs?更新它的内容我尝试过,但没有成功。。。如果我使用的是友好的URL,这并不重要?。。。实际上comments.php也发布了评论。在comments.php中,我试图用$id=intval($_get['n'])获取id;但它也不起作用