Php codeigniter通过Ajax请求从控制器返回要查看的数据
我通过ajax JSON请求加载了一个视图,在该视图中我想显示数据库中的fetch记录,但它没有显示记录 这是我的查看代码Php codeigniter通过Ajax请求从控制器返回要查看的数据,php,mysql,json,ajax,codeigniter,Php,Mysql,Json,Ajax,Codeigniter,我通过ajax JSON请求加载了一个视图,在该视图中我想显示数据库中的fetch记录,但它没有显示记录 这是我的查看代码 <div class="col-md-6" id="hodm_table"> <table class="table table-striped table-hover table-responsive"> <thead> <tr> <th>Task Name</th>
<div class="col-md-6" id="hodm_table">
<table class="table table-striped table-hover table-responsive">
<thead>
<tr>
<th>Task Name</th>
<th>Director</th>
<th>Duration</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach($result as $hodm) { ?>
<tr>
<td><?php echo $hodm->t_name;?></td>
<td><?php echo $hodm->director;?></td>
<td><?php echo $hodm->duration;?></td>
<td><?php echo $hodm->status;?></td>
<?php } ?>
</tbody>
</table>
</div>
</div>
<script type='text/javascript' language='javascript'>
$(document).ready(function(){
$.ajax({
url:"<?php echo base_url();?>digital/dashboard/dig_short_hodm_table",
type: 'POST',
dataType: 'JSON',
success:function (data) {
$('#hodm_table').html(data);
}
});
event.preventDefault();
});
</script>
这是我的控制器
public function dig_short_hodm_table(){
$data['result']=$this->pojo->get_hodm();
return json_encode($data);
}
当我加载页面时,它会显示错误
Message: Undefined variable: result
我想在视图加载时从数据库中获取记录并显示在视图表中。您必须这样更改
public function get_hodm()
{
return $this->db->get("hodm")->result();
}
你必须这样改变
public function get_hodm()
{
return $this->db->get("hodm")->result();
}
更新您的模型:
public function get_hodm(){
return $this->db->get("hodm")->result();
}
您的控制器:
public function dig_short_hodm_table(){
$result_html = '';
$result_set = $this->pojo->get_hodm();
foreach($result_set as $result) {
$result_html .= '
<tr>
<td>' . $result->t_name . '</td>
<td>' . $result->director . '</td>
<td>' . $result->duration . '</td>
<td>' . $result->status . '</td>
</tr>';
}
echo json_encode($result_html);
}
<script type='text/javascript' language='javascript'>
$(document).ready(function(){
$.ajax({
url:"<?php echo base_url();?>digital/dashboard/dig_short_hodm_table",
type: 'POST',
dataType: 'JSON',
success:function (data) {
var result = data.result;'
var row = "";
for(i=0; i < result.length; i++) {
row += "<tr>";
row += "<td>"+result[i].t_name+"</td>";
row += "<td>"+result[i].director+"</td>";
row += "<td>"+result[i].duration+"</td>";
row += "<td>"+result[i].status+"</td>";
row += "</tr>";
}
$('#hodm_table > tbody').html(row);
}
});
event.preventDefault();
});
</script>
公共函数dig\u short\u hodm\u table(){
$result_html='';
$result\u set=$this->pojo->get\u hodm();
foreach($result\u设置为$result){
$result\u html.='
“.$result->t_name”
“.$result->director”
“.$result->duration”
“.$result->status”
';
}
echo json_encode($result_html);
}
最后,你的看法是:
<div class="col-md-6" id="hodm_table">
<table class="table table-striped table-hover table-responsive">
<thead>
<tr>
<th>Task Name</th>
<th>Director</th>
<th>Duration</th>
<th>Status</th>
</tr>
</thead>
<tbody id="hodm_results">
</tbody>
</table>
</div>
<script type='text/javascript' language='javascript'>
$(document).ready(function(){
$.ajax({
url:"<?php echo base_url();?>digital/dashboard/dig_short_hodm_table",
type: 'POST',
dataType: 'JSON',
success:function (data) {
$('#hodm_results').html(data);
}
});
event.preventDefault();
});
</script>
任务名称
经理
期间
地位
$(文档).ready(函数(){
$.ajax({
url:“数字/仪表板/数据表”,
键入:“POST”,
数据类型:“JSON”,
成功:功能(数据){
$('hodm_results').html(数据);
}
});
event.preventDefault();
});
更新您的型号:
public function get_hodm(){
return $this->db->get("hodm")->result();
}
您的控制器:
public function dig_short_hodm_table(){
$result_html = '';
$result_set = $this->pojo->get_hodm();
foreach($result_set as $result) {
$result_html .= '
<tr>
<td>' . $result->t_name . '</td>
<td>' . $result->director . '</td>
<td>' . $result->duration . '</td>
<td>' . $result->status . '</td>
</tr>';
}
echo json_encode($result_html);
}
<script type='text/javascript' language='javascript'>
$(document).ready(function(){
$.ajax({
url:"<?php echo base_url();?>digital/dashboard/dig_short_hodm_table",
type: 'POST',
dataType: 'JSON',
success:function (data) {
var result = data.result;'
var row = "";
for(i=0; i < result.length; i++) {
row += "<tr>";
row += "<td>"+result[i].t_name+"</td>";
row += "<td>"+result[i].director+"</td>";
row += "<td>"+result[i].duration+"</td>";
row += "<td>"+result[i].status+"</td>";
row += "</tr>";
}
$('#hodm_table > tbody').html(row);
}
});
event.preventDefault();
});
</script>
公共函数dig\u short\u hodm\u table(){
$result_html='';
$result\u set=$this->pojo->get\u hodm();
foreach($result\u设置为$result){
$result\u html.='
“.$result->t_name”
“.$result->director”
“.$result->duration”
“.$result->status”
';
}
echo json_encode($result_html);
}
最后,你的看法是:
<div class="col-md-6" id="hodm_table">
<table class="table table-striped table-hover table-responsive">
<thead>
<tr>
<th>Task Name</th>
<th>Director</th>
<th>Duration</th>
<th>Status</th>
</tr>
</thead>
<tbody id="hodm_results">
</tbody>
</table>
</div>
<script type='text/javascript' language='javascript'>
$(document).ready(function(){
$.ajax({
url:"<?php echo base_url();?>digital/dashboard/dig_short_hodm_table",
type: 'POST',
dataType: 'JSON',
success:function (data) {
$('#hodm_results').html(data);
}
});
event.preventDefault();
});
</script>
任务名称
经理
期间
地位
$(文档).ready(函数(){
$.ajax({
url:“数字/仪表板/数据表”,
键入:“POST”,
数据类型:“JSON”,
成功:功能(数据){
$('hodm_results').html(数据);
}
});
event.preventDefault();
});
我有更新的模型视图和控制器:
public function dig_short_hodm_table(){
$result_html = '';
$result_set = $this->pojo->get_hodm();
foreach($result_set as $result) {
$result_html .= '
<tr>
<td>' . $result->t_name . '</td>
<td>' . $result->director . '</td>
<td>' . $result->duration . '</td>
<td>' . $result->status . '</td>
</tr>';
}
echo json_encode($result_html);
}
<script type='text/javascript' language='javascript'>
$(document).ready(function(){
$.ajax({
url:"<?php echo base_url();?>digital/dashboard/dig_short_hodm_table",
type: 'POST',
dataType: 'JSON',
success:function (data) {
var result = data.result;'
var row = "";
for(i=0; i < result.length; i++) {
row += "<tr>";
row += "<td>"+result[i].t_name+"</td>";
row += "<td>"+result[i].director+"</td>";
row += "<td>"+result[i].duration+"</td>";
row += "<td>"+result[i].status+"</td>";
row += "</tr>";
}
$('#hodm_table > tbody').html(row);
}
});
event.preventDefault();
});
</script>
这是我的控制器
public function dig_short_hodm_table(){
$data['result']=$this->pojo->get_hodm();
echo json_encode($data);
}
我已更新模型视图和控制器:
public function dig_short_hodm_table(){
$result_html = '';
$result_set = $this->pojo->get_hodm();
foreach($result_set as $result) {
$result_html .= '
<tr>
<td>' . $result->t_name . '</td>
<td>' . $result->director . '</td>
<td>' . $result->duration . '</td>
<td>' . $result->status . '</td>
</tr>';
}
echo json_encode($result_html);
}
<script type='text/javascript' language='javascript'>
$(document).ready(function(){
$.ajax({
url:"<?php echo base_url();?>digital/dashboard/dig_short_hodm_table",
type: 'POST',
dataType: 'JSON',
success:function (data) {
var result = data.result;'
var row = "";
for(i=0; i < result.length; i++) {
row += "<tr>";
row += "<td>"+result[i].t_name+"</td>";
row += "<td>"+result[i].director+"</td>";
row += "<td>"+result[i].duration+"</td>";
row += "<td>"+result[i].status+"</td>";
row += "</tr>";
}
$('#hodm_table > tbody').html(row);
}
});
event.preventDefault();
});
</script>
这是我的控制器
public function dig_short_hodm_table(){
$data['result']=$this->pojo->get_hodm();
echo json_encode($data);
}
通过更改标题内容,尝试像这样返回控制器
public function dig_short_hodm_table(){
header('Content-Type: application/x-json; charset=utf-8');
$data['result']=$this->pojo->get_hodm();
echo(json_encode($data));
}
通过更改标题内容,尝试像这样返回控制器
public function dig_short_hodm_table(){
header('Content-Type: application/x-json; charset=utf-8');
$data['result']=$this->pojo->get_hodm();
echo(json_encode($data));
}
改变你的模型如下
改变你的模型如下
在控制器功能中加载视图页@xr33dx@Parvez无法在控制器功能中加载视图页@xr33dx@Parvez它不起作用告诉我问题或消息是什么?我知道问题,您只需更改$data['result']使用$datafirst时,此消息消息消息:未定义变量:结果闪烁,然后显示空白页面。更改此行以返回json_encode($data);输入echo json_encode($data);你改变了什么,传达了什么信息,你知道吗?要检测您的错误?告诉我问题或消息是什么?我知道问题您只需将$data['result']更改为$data首先此消息消息:未定义变量:结果闪烁,然后显示空白页面哦更改此行以返回json_encode($data);输入echo json_encode($data);你改变了什么,传达了什么信息,你知道吗?要检测您的错误?您能给我一个您收到的描述性错误消息吗?遇到了一个PHP错误严重性:注意消息:未定义变量:结果文件名:digital/dashboard.PHP行号:38回溯:文件:C:\xampp\htdocs\tmt\u project\application\views\digital\dashboard.PHP行:38函数:\错误\处理程序文件:C:\xampp\htdocs\tmt\u project\application\controllers\digital\dashboard.php行:17函数:查看文件:C:\xampp\htdocs\tmt\u project\index.php行:292函数:require\u once我将添加另一个答案给我几秒钟的时间,谢谢。我在等,请点击我的问题。我已经记下了你的答案。。Thanx for Help您能给我一条您收到的描述性错误消息吗?遇到PHP错误严重性:注意消息:未定义变量:结果文件名:digital/dashboard.PHP行号:38回溯:文件:C:\xampp\htdocs\tmt\u project\application\views\digital\dashboard.PHP行:38函数:\错误\处理程序文件:C:\xampp\htdocs\tmt\u project\application\controllers\digital\dashboard.php行:17函数:查看文件:C:\xampp\htdocs\tmt\u project\index.php行:292函数:require\u once我将添加另一个答案给我几秒钟的时间,谢谢。我在等,请点击我的问题。我已经记下了你的答案。。Thanx需要帮助您可以在控制台中签入吗?我得到了答案谢谢你的帮助。我更喜欢UG的回答你能在控制台上检查一下吗?我得到了答案谢谢你的帮助。我更喜欢你的答案