如何使用codeigniter从选择框中选择值时获取表数据

时间:2016-06-07 03:50:36

标签: php jquery mysql codeigniter

View.php

这是codeigniter中的视图文件。

<div class="table-responsive">
    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>#</th>
                <th>Year Range</th>
                         <select>
                         <?php foreach ($result as $value): ?>
                          <td></td>
                <option><?php echo $value['year_key'] ?></option>
              </select>

                <th>Species</th>
                <th>Data</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>
        </thead>
        <tbody>

            <?php foreach ($result as $value): ?>
                <tr>
                    <td></td>
                    <?php foreach ($value['speces_key'] as $key => $value2): ?>

                        <td><?php echo $key; ?></td>
                        <td><?php echo $value2['data'] ?></td>

                    <?php endforeach; ?>
                </tr>

            <?php endforeach; ?>
        </tbody>
    </table>
</div>

我的问题:我点击年份范围选择框并从选择框中选择值并使用codeigniter根据选择值获取表格数据

1 个答案:

答案 0 :(得分:0)

请使用ajax调用更改年份范围,如

<div class="table-responsive">
    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>#</th>
                <th>Year Range</th>
                         <select id="year">
                         <?php foreach ($result as $value): ?>
                          <td></td>
                <option><?php echo $value['year_key'] ?></option>
              </select>

                <th>Species</th>
                <th>Data</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>
        </thead>
        <tbody>

            <?php foreach ($result as $value): ?>
                <tr>
                    <td></td>
                    <?php foreach ($value['speces_key'] as $key => $value2): ?>

                        <td><?php echo $key; ?></td>
                        <td><?php echo $value2['data'] ?></td>

                    <?php endforeach; ?>
                </tr>

            <?php endforeach; ?>
        </tbody>
    </table>
</div>
<script>
$('#year').on('change',function () {
        var year = $('#year').val();
        $.ajax({
            url: 'url for get data',
            type: 'POST',
            data: {year: year},
            success: function (a) {
                data = JSON.parse(a);
                #get data and print data with each loop
            }
        });
    });
</script>