【问题标题】:CodeIgniter form_submit and aJax use to create data tableCodeIgniter form_submit 和 aJax 用于创建数据表
【发布时间】:2013-11-15 13:07:54
【问题描述】:
如何创建如下系统:
在表单中,我有 2 个输入(“日期来自”和“日期至”),在单击“提交”按钮后,带有数据的表(通过使用设置了日期的数据库查询收集)。我用 PHP 制作了一切,一切正常,但我不知道如何为它实现 aJax。我需要aJax的原因是表格必须在提交日期后出现在同一页面中(我还设置了默认日期(今天的日期),不需要设置日期)。
我认为,我的代码是必要的,但如果需要,请询问,我会给它。
那么问题来了,怎么可能在那里实现 aJax?
【问题讨论】:
标签:
php
ajax
forms
codeigniter
submit
【解决方案1】:
使用 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 索引.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>