【问题标题】:How to change displayed html content in view using jquery - codeigniter如何使用 jquery 更改视图中显示的 html 内容 - codeigniter
【发布时间】: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> &nbsp;<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


【解决方案1】:

你应该使用load()

试试这个:

 function autoRefresh_div()
 {
      $("#newTable").load("tableLoad.php");// a function which will load data from other file after x seconds
  }

  setInterval('autoRefresh_div()', 2000); // refresh div after 2 secs

然后使用这个:

    <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>
         <div class="fix"><ul id="newTable">
            <?php include 'tableLoad.php' ?>   
         </ul>         
         </div>   
            <p class="viewall"><a href="#">view more </a> &nbsp;<i class="fa fa-hand-o-right"></i></p>
          </div>

仅从tableLoad.php 获取&lt;li&gt;

同样,您可以使用$.ajax()$.post() 等代替load() 函数并将响应放入divul

$.ajax()

     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
   <script>
 function autoRefresh_div()
 {

    $.ajax({
            dataType:'text',
            type: "POST",
            url: 'url_to_your_file or function',
            success: function (response){

               $("#newTable").html(response);

              }
        });
  }

  setInterval('autoRefresh_div()', 2000); // refresh div after 2 secs
            </script>

【讨论】:

  • 感谢您的回答。我之前尝试过这个解决方案,但它给了我 404 错误,因为文件不在同一个目录中。当我尝试使用 tableLoad.php 的路径加载它时,它给了我 403 错误被禁止。这可能是因为 codeigniter 结构,但不知道如何解决这个问题。
  • 那么你应该使用$.ajax()。它更好更高效
  • 我还不太明白如何使用ajax。你能给我一些例子如何用ajax解决这个问题吗?
  • 我用它作为文件的 url 路径。网址:BASE_URL+'application/view/loadTable.php'。但仍然出现 403 禁止错误
  • BASE_URL 是一个 php 变量,所以在 php 标签中使用它url: '&lt;?php echo BASE_URL; ?&gt;application/view/loadTable.php'
猜你喜欢
  • 2013-05-08
  • 2013-04-23
  • 2023-03-11
  • 2013-11-02
  • 2017-03-13
  • 1970-01-01
  • 1970-01-01
  • 2011-02-07
  • 2019-06-15
相关资源
最近更新 更多