【发布时间】:2023-04-04 06:59:02
【问题描述】:
我不太确定如何输入这个问题的标题,但无论如何
我的视图中有这样的代码,称为 content.php
<div class="fix single_content floatleft">
<h2><img src="<?php echo base_url('assets/images/New.png')?>" alt="" width="24" height="24"></img><a href="<?php echo base_url('index.php/Main_Controller/loadKeluhan')?>" >New</a> </h2>
<?php include 'tableLoad.php' ?>
<p class="viewall"><a href="#">view more </a> <i class="fa fa-hand-o-right"></i></p>
</div>
在 tableLoad.php 中看起来像这样
<div class="fix"><ul id="newTable"><?php
$i=0;
while($i<count($array)){
echo '<li>
<div class="entry_name floatleft"><a href="pages/single.html"><img src="assets/images/mozilla.png" alt="">'.$array[$i].'</a></div>
<div class="download-count floatright"><i class="fa fa-download"></i> 4578</div>
</li>';
$i++;
}
?></ul></div>
$array[i] 是来自我的控制器的转发变量,其中包含我需要从我的数据库中获取的项目列表。
我希望它每 2 秒刷新一次,以便在数据库发生更改时更改显示。我在 jQuery 中使用我的代码来做到这一点:
$(document).ready(function(){setInterval(function(){
$('#newTable').html("<?php include 'tableLoad.php'; ?>")
}, 2000);});
问题是每次运行 jQuery 代码时,它什么都不显示,浏览器控制台也没有错误报告。 php中的代码运行良好,但jQuery中的代码运行不佳。
谁能给我一些启示?
在此先感谢,对我的英语不好感到抱歉
已解决
解决方案:
我将我的 js 代码更改为 Ajax,并像 poonam 所说的那样从控制器调用函数。 js文件变成这样:
$(document).ready(function(){
setInterval('autoRefresh_div()', 2000);
});
function autoRefresh_div()
{
$.ajax({
dataType:'text',
type: "POST",
url: BASE_URL+'index.php/Main_Controller/loadKeluhan',
success: function (response){
$("#newTable").html(response);
}
});}
使用 loadKeluhan 是我的 Main_Controller 中的一个函数,用于加载 tableLoad 视图并将新的数组值传递给它。
【问题讨论】:
-
你需要在控制器中加载视图,然后在ajax中使用这个url。
标签: javascript php jquery html codeigniter