CodeIgniter form_submit 和 aJax 用于创建数据表


CodeIgniter form_submit and aJax use to create data table

如何创建如下系统:在表单中,我有 2 个输入("日期开始"和"日期到"),然后单击"提交"按钮后,带有数据的表格(这是通过使用设置日期的数据库查询收集的)。我用PHP做了所有东西,一切正常,但我不知道如何为它实现aJax。我需要 aJax 的要点是,在提交日期后,表格必须在同一页面中出现(我还设置了默认日期(今天的日期),无需设置日期)。

我想,我的代码是必要的,但如果有需要,请问,我会给的。那么,问题是,如何在那里实现aJax?

使用 JQuery 你可以做这样的事情

PHP generate_table.php

<?php
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];
    //Here goes your table generation script
    $table = sprintf('<p>%s - %s</p>',$start_date,$end_date); //Generated table (here's just a p for simplicity
    echo json_encode(array('table' => $table));     
?>

网页索引.html

<form id="dates">
    <input name="start_date" />
    <input name="end_date" />
    <input type="submit" id="submit_date" />
</form>
<div id="generated_table"></div>
<script>
    //We generate the event when they click the submit button
    $('#submit_date').click(function(e){
        e.preventDefault();//Prevent default behaviour
        $.ajax({
            type: 'POST',
            url: 'generate_table.php',
            data:$('#dates').serialize(), //Sending the dates as post parameters
            success:function(data){
                $('#generated_table').html(data.table); //Output the table in the div
            }
        })
    })
</script>