【问题标题】:Jquery Ajax Calls With CodeIgniter使用 CodeIgniter 的 Jquery Ajax 调用
【发布时间】:2013-03-17 14:28:11
【问题描述】:

我正在尝试为我的页面上的两个表格设置一种自动更新的方式,或者如果我“异步”错误地纠正我。我也需要第二次更新。我也在使用 CodeIgnighter MVC 框架,至于我的表,我使用的是 DataTables。我的 Jquery 如下:

<script>
        function updatewaiting(){
        $('.display').load('https://www.finaidtest.com/index.php/studentqueue_controller/data');
        }
        setInterval( "updatewaiting()", 1000 );

    </script>

我的表格如下:

<table id='waiting' class='display'>
        <thead>
        <tr>
            <th>ID</th>
            <th>A Number</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Time Waiting</th>
            <th>Reason for visit</th>
            <th>Comments</th>
            <th>Aid Year</th>
            <th>Staff Comments</th>
            <th>Options</th>
        </tr>
        </thead>
        <tbody>

        <?php
        $options = array("" => "", 'start' => 'Start Session', 'stop' => 'Student Not Present', 'abandon' => 'End Session',);
        foreach ($waiting as $row) {
            ?>
            <tr>
                <td><?php echo htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td><?php echo anchor('studentqueue_controller/history/' . urlencode($row['anum']) . '', htmlspecialchars($row['anum'], ENT_QUOTES, 'UTF-8'), 'target="_blank"'); ?></td>
                <td><?php echo htmlspecialchars($row['first'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td><?php echo htmlspecialchars($row['last'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td><?php echo htmlspecialchars($row['SECOND'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td><?php echo htmlspecialchars($row['reason'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td><?php echo htmlspecialchars($row['studentcomments'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td><?php echo htmlspecialchars($row['aidyear'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td><?php echo htmlspecialchars($row['counselorcomments'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td>
                <?php echo form_open('studentqueue_controller/counselorscreen/' . urlencode($row['id']) . ''); ?>
                <?php echo form_dropdown('options', $options, ""); ?>
                <?php echo form_submit('submit', 'Submit'); ?>
                <?php echo form_close(); ?>
                </td>
            </tr>

            <?php }
        ?>
        </tbody>
    </table>

<table id='beingseen' class='display'>
        <thead>
        <tr>
            <th>ID</th>
            <th>A Number</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Sign In Time</th>
            <th>Staff Member</th>
            <th>Start Time</th>
            <th>Options</th>
        </tr>
        </thead>
        <tbody>
        <?php $options1 = array("" => "", 'continue' => 'Continue Session', 'terminate' => 'Terminate Session',);
        foreach ($beingseen as $row1) {
            ?>
            <tr>
                <td><?php echo htmlspecialchars($row1['id'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td><?php echo htmlspecialchars($row1['anum'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td><?php echo htmlspecialchars($row1['first'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td><?php echo htmlspecialchars($row1['last'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td><?php echo htmlspecialchars($row1['signintime'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td><?php echo htmlspecialchars($row1['fname'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td><?php echo htmlspecialchars($row1['starttime'], ENT_QUOTES, 'UTF-8'); ?></td>
                <td>
                <?php echo form_open('studentqueue_controller/counselorscreen/' . urlencode($row1['id']) . ''); ?>
                <?php echo form_dropdown('options', $options1, ""); ?>
                <?php echo form_submit('submit', 'Submit'); ?>
    <?php echo form_close(); ?>
                </td>
                </td>
            </tr>
<?php } ?>


        </tbody>
    </table>

Jquery 函数在我的控制器中调用我的 data() 方法,所做的只是查询数据库:

function data(){
    $this->load->model('queue_model');

    $data['waiting'] = $this->queue_model->waiting();
    $data['beingseen'] = $this->queue_model->beingseen();
    }

我很困惑为什么当我查看我的网络选项卡时我看到了 ajax 调用,但我没有在网络选项卡中看到响应。

只是一个更新日期:

Array ( [waiting] => Array ( [0] => Array ( [id] => 176 [0] => 176 [anum] => A00163047 [1] => A00163047 [first] => rix [2] => rix [last] => aja [3] => aja [SECOND] => 62:47:39 [4] => 62:47:39 [reason] => Award Status [5] => Award Status [studentcomments] => [6] => [aidyear] => 13-14 [7] => 13-14 [counselorcomments] => [8] => ) [1] => Array ( [id] => 179 [0] => 179 [anum] => A00163047 [1] => A00163047 [first] => rix [2] => rix [last] => aja [3] => aja [SECOND] => 00:22:29 [4] => 00:22:29 [reason] => Award Status [5] => Award Status [studentcomments] => [6] => [aidyear] => 13-14 [7] => 13-14 [counselorcomments] => [8] => ) [2] => Array ( [id] => 178 [0] => 178 [anum] => A12345678 [1] => A12345678 [first] => ririri [2] => ririri [last] => ririir [3] => ririir [SECOND] => 00:24:21 [4] => 00:24:21 [reason] => Award Status [5] => Award Status [studentcomments] => [6] => [aidyear] => 13-14 [7] => 13-14 [counselorcomments] => [8] => ) [3] => Array ( [id] => 177 [0] => 177 [anum] => A99999999 [1] => A99999999 [first] => rux [2] => rux [last] => ajaj [3] => ajaj [SECOND] => 00:29:20 [4] => 00:29:20 [reason] => Award Status [5] => Award Status [studentcomments] => [6] => [aidyear] => 13-14 [7] => 13-14 [counselorcomments] => [8] => ) ) [beingseen] => )

我的数据函数实际上是将数据返回给控制器。我刚刚对我的数据做了一个 print_r 。然而,返回的数据数组被放入表本身,

【问题讨论】:

  • 您的数据函数没有返回任何内容。
  • 是的,它正在返回到控制器,但控制器没有将它返回到视图。没有返回,没有打印,视图永远不会看到该数据。

标签: php codeigniter jquery


【解决方案1】:

在您的情况下,您应该使用 load() 在 javascript 中加载视图并打印它,因此请使用:

function data(){
        $this->load->model('queue_model');

        $data['waiting'] = $this->queue_model->waiting();
        $data['beingseen'] = $this->queue_model->beingseen();

        echo $this->load->view('myview',$data,true);
    }

您的视图将输出到您指定$(_element).load();的位置

只是为了确保你理解 load() jquery 方法:

当你做$('div#element').load('myfile.php'); 你就是outputtting the returning data from myfile.php into the &lt;div id="element"&gt;&lt;/div&gt; html 元素

【讨论】:

  • 仍然注意到网络选项卡中显示,以及我如何处理 json_encoded 数据?把它扔到视野里?
  • 如果我这样做,那么表格会不断弹出来破坏页面的视图。
  • @Joe 记住,load() --->是你想要输出的内容$('.display') --->是你在哪里将输出 ;)
  • 对不起?我对jquery非常了解
  • @Joe 没问题,当你这样做 $('#element').load('myfile.php');您正在将 myfile.php 中的返回数据输出到 id="element" html 元素
猜你喜欢
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 2015-05-08
  • 2012-12-21
  • 2014-04-05
  • 2011-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多