【发布时间】:2020-03-16 14:34:21
【问题描述】:
我正在尝试获取与我的输入隐藏值具有相同 id 的数据。
我无法获取数据的 id 或输入隐藏值
首先,这是为了让我更清楚地调试我所做的事情,我制作了一个硬编码表单和一个按钮来将我带到页面
在视图中 - groups/index.php
如您所见,我创建了
posts/index/1,这是一个硬编码值,但我可以稍后轻松更改,这不是我的问题
<?php echo form_open('posts/index/1') ?>
<input type="hidden" name="txtGroup" value="1" />
<button type="submit" class="btn btn-info"> Submit</button>
</form>
所以在我制作完表单后,我将在控制器中创建一个函数来获取posts/index
在控制器中 - Posts.php
public function index(){
$this->load->view('templates/header');
$this->load->view('teachers/posts/index');
$this->load->view('templates/footer');
}
于是我获取了页面。至此,我现在可以通过/posts/index/1 看到我的页面
在我的页面 posts/index.php 我有一个数据,它是通过 Ajax 获取的,这里是
所以我已经在
posts/showPosts中获取了这些数据
showAllQuestions();
//Show all data
function showAllQuestions(){
$.ajax({
type: 'ajax',
url: '<?php echo base_url() ?>posts/showPosts',
async: false,
dataType: 'json',
success: function(data){
var html = '';
var i;
var n=1;
for(i=0; i<data.length; i++){
html +='<div class="card">'+
'<div class="card-header" style="color:white; background-color:black">'+
'<h4><span class="iconify" data-icon="ant-design:info-circle-outlined" data-inline="false"></span> Question No. '+ n++ +'</h4>'+
'</div>'+
'<div class="card-body">'+
'<form>'+
'<div class="form-group">'+
'<label for="exampleFormControlTextarea1"><h5> <span class="iconify" data-icon="emojione:check-mark-button" data-inline="false"></span> </h5></label>'+
'<input type="text" value="'+data[i].question+'" class="form-control" disabled />'+
'</div>'+
'<hr>'+
'<a href="<?php echo base_url() ?>posts/edit/'+data[i].id+'" class="btn btn-info"><span class="iconify" data-icon="el:edit" data-inline="false"></span> </a> '+
'<a href="<?php echo base_url()?>posts/delete/'+data[i].id+'" class="btn btn-primary"><span class="iconify" data-icon="fa-solid:trash-alt" data-inline="false"></span></a>'+
// '<a href="javascript:;" class="btn btn-info item-edit" data-id="'+data[i].id+'">Edit </a> '+
'</form>'+
'</div>'+
'</div><br>';
}
$('#showdata').html(html);
},
error: function(){
alert('Could not get Data from Database');
}
});
}
在posts/showPosts - Posts.php,这是控制器
public function showPosts(){
$result = $this->post_model->showPosts();
echo json_encode($result);
}
最后是模型来确定我是否根据我提交的数据 id 获取了正确的 ID
问题是 $id 为空,我不知道如何开始,因为我在视图页面上声明了一个隐藏的输入值。
public function showPosts(){
// Show questions and answers
$id = $this->input->post('txtGroup');
$this->db->select('*');
$this->db->from('questions');
$this->db->where('group_id', $id);
$query = $this->db->get();
return $result = $query->result_array();
}
【问题讨论】:
标签: php ajax codeigniter codeigniter-3