gpt4 book ai didi

mysql - 使用 jQuery AJAX 请求 CodeIgniter 在不刷新页面的情况下从 Controller 显示数据库中的数据到 View

转载 作者:搜寻专家 更新时间:2023-10-30 20:52:59 29 4
gpt4 key购买 nike

如何在 Codeigniter 中加载页面的特定部分? Ajax 正在工作,但它通过调用 Controller 名称(如页眉、正文、页脚)重新加载所有 View ,但我只想重新加载正文,我如何分离代码才能正常工作。

它的 Ajax 代码

$('label#showdata').click(function(){

var fromdate =document.getElementById('fromdate').value;
var todate = $(this).val();
var dataString = 'fromdate='+ fromdate+'todate='+todate;

$.ajax({
type: "POST",
url: "https://chotigaadi.com/myadmin/references",
data: dataString,
cache: false,
success: function(html)
{
$("div#result").html(html).show();
//alert(dataString);
}
});
});

这是我的看法

<div class="col s12 m12">
<form action="<?php echo base_url();?>myadmin/ExportCSV" method="POST">
<div class="col s12 m3">
<h5 class="center" style="color:green;font-weight:bold;">Registered Members List</h5>
</div>
<div class="col s12 m2">
<div class="input-field col s12">
<label for="date"> <i class="fa fa-calendar"></i> Select from date below:</label>
</br>
<input name="fromdate" id="fromdate" type="date" class="datepicker" required>

</div>
</div>
<div class="col s12 m3">
<div class="input-field col s12">
<label for="date"> <i class="fa fa-calendar"></i> Select to date below:</label>
</br>
<input name="todate" id="todate" type="date" class="datepicker" required>

</div>

</div>
<div class="col s12 m4">
<div class="col s6">
<label id="showdata" class="btn waves-effect waves-light orange right" value="Show">Show Results</label>
</div>
<div class="col s6">
<input type="submit" id="datesubmit" class="btn waves-effect waves-light green right" value="Export Data" />
</div>
</div>
</form>
<div id="result">
<table class="highlight">
<thead>
<tr style="background-color:#ccc;">
<th data-field="name">ID</th>
<th data-field="name">Referee</th>
<th data-field="rname">Reference</th>
<th data-field="email">Email</th>
<th data-field="phone">Phone</th>

</tr>
</thead>
<tbody>
<?php
foreach ($results as $row)
{?>
<tr style="">

<td>
<?php echo $row->firstname;?>
</td>
<td>
<?php echo $row->name; ?>
</td>
<td>
<?php echo $row->email; ?>

</td>
<td>
<?php echo $row->phone; ?>
</td>
</tr>
<?php }?>
</tbody>
</table>
</div>
</div>

我的 Controller 代码:

function references(){
if (!$this->check_user()) {
redirect('myadmin/login/');
}
else
{
if(isset($_POST['fromdate']) && isset($_POST['todate']))
{
$fromdate=new DateTime(str_replace("-","",$_POST['fromdate']));
$fromdate=$fromdate->format('Y-m-d H:i:s');
$todate=new DateTime(str_replace("-","",$_POST['todate']));
$todate=$todate->format('Y-m-d H:i:s');
}
else
{
$fromdate=new DateTime('2015-12-10 14:28:27');
$fromdate=$fromdate->format('Y-m-d H:i:s');
$todate=new DateTime(date("Y-m-d"));
$todate=$todate->format('Y-m-d H:i:s');
}


$config = array();
$config["base_url"] = base_url() . "myadmin/references";
$config["total_rows"] = $this->myadmin_model->record_count($fromdate, $todate);
$config["per_page"] = 8;
$config["uri_segment"] = 3;

$this->pagination->initialize($config);

$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->myadmin_model->fetch_userdata($config["per_page"], $page, $fromdate, $todate);
$data["links"] = $this->pagination->create_links();



$this->load->view('/admin/myadminheader');
$this->load->view('/admin/myadmin',$data);
$this->load->view('/admin/myadminfooter');
}
}

我只想重新加载 View $this->load->view('/admin/myadmin',$data); by ajax 我该怎么做?

最佳答案

如果您只想更改中间部分,请不要在 ajax 调用函数中加载页眉和页脚。

删除这两行:

$this->load->view('/admin/myadminheader'); 
$this->load->view('/admin/myadminfooter');

它只是:

$this->load->view('/admin/myadmin',$data); 

这只会在 ajax 响应中返回中间部分而不是页眉页脚。

旁注:

如果您直接从某个地方使用此函数,则需要传递来自 ajax 请求的任何参数,以检查是否来自 ajax 请求。

关于mysql - 使用 jQuery AJAX 请求 CodeIgniter 在不刷新页面的情况下从 Controller 显示数据库中的数据到 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35232324/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com