php - Jquery Ajax 使用 CodeIgniter 进行调用

标签 php codeigniter jquery

我正在尝试设置一种方法,使页面上的两个表格自动更新,或者如果我“异步”错了,请纠正我。我也需要每秒更新。我还使用 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 函数正在调用我的 Controller 中的 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] => )

我的数据函数实际上是将数据返回到 Controller 。我刚刚对我的数据做了 print_r 。然而,返回的数据数组被放置到表本身中,

最佳答案

在您的情况下,您应该加载 View 并使用 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);
    }

并且您的 View 将输出到您用 $(_element).load(); 指定的位置

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

当你这样做时$('div#element').load('myfile.php');你是outputtting the returning data from myfile.php into the <div id="element"></div> html元素

关于php - Jquery Ajax 使用 CodeIgniter 进行调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15461857/

相关文章:

php - 在 PHP 中解析 Wikipedia API json

php GD 为图像添加填充

php - 使用 PHP 和 XPATH,如何获取最接近的 `h3` 的内容?

javascript - 简单的 jQuery 函数在创建后不适用于输入元素

javascript - 如何获取div的精确宽度和高度

php - 如何为给定场景创建 SQL 查询?

php - 直接从 codeigniter 中的 View 使用模型不好吗?

php - 我构建的每个项目都需要有一个 CI 文件夹吗?

javascript - jQuery : How to handle event of multiple elements

javascript - 如何将键盘绑定(bind)添加到轮播功能? -jQuery