[英]Code with CodeIgniter on one page?
我真的不知道如何在CodeIgniter项目中组织代码。 我认为,我有一个表格,可以在提交后获得信息。 但是信息显示在同一视图上。 信息以多种方式显示(例如在控制器部分中使用PHP或Ajax和JSON编码)。
我的观点 :
<form action="#" method="post">
<p>Your name : <input type="text" name="name" /></p>
<p><input type="submit" value="OK"></p>
</form>
单击提交按钮后我要显示的内容:
<select class="mylist">
<?php foreach($groups as $each){ ?>
<option value="<?php echo $each->groupname; ?>"><?php echo $each->groupname; ?></option>';
<?php } ?>
</select>
<table id="table" class="display" style="width:80%">
<thead>
<tr>
<th>Name</th>
<th>SurName</th>
<th>ID</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>SurName</th>
<th>ID</th>
</tr>
</tfoot>
</table>
控制器:
public function index()
{
$this->load->view("myview.php");
}
public function getlist
{
$this->load->model('mymodel');
// Method to get the values of the list in the database
}
public function get_test_datatables()
{
// Method to fill the datatable part
echo json_encode($output);
}
JS函数:
$(document).ready( function () {
$('#table').DataTable({
//Get the data with ajax
})
)}
按提交按钮后,我还要检查是否已选中一个复选框(如果在数据库中等于true,则必须选中该复选框,如果它等于false,则不必选中它) 。 我是否可以在视图中执行以下操作:
if(checkbox->value == true)
{
<input type="checkbox" name="vehicle" value="Bike" checked> I have a bike<br>
}
else {
<input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
}
视图
<form action="#" method="post" id="frmsubmit">
<p>Your name : <input type="text" name="name" /></p>
<p><input type="submit" value="OK"></p>
</form>
<select class="mylist">
<?php foreach($groups as $each){ ?>
<option value="<?php echo $each->groupname; ?>"><?php echo $each->groupname; ?></option>';
<?php } ?>
</select>
<table id="table" class="display" style="width:80%">
<thead>
<tr>
<th>Name</th>
<th>SurName</th>
<th>ID</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>SurName</th>
<th>ID</th>
</tr>
</tfoot>
</table>
JS
$(document).ready( function () {
loaddata();
$('#frmsubmit').on('submit', function (e) {
$.ajax({
url:$(this).attr('action'),
type:'post',
dataType:'json',
data:$(this).serialize(),
success: function (data) {
loaddata();
}
});
});
function loaddata()
{
$('#table').DataTable({
"ajax": '<?= site_url('url') ?>'
});
}
});
数据库代码在模型中,方法在控制器中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.