Ae9*_*e98 2 javascript php jquery codeigniter .post
我正在使用codeigniter,我有一个问题,使用jQuery $.post
函数处理数据.我想给这样的值,subjectid
以ajax_get_subject_credit同一个数据库表中的功能和检索另一个领域.结果显示在另一个文本字段中.这是我的代码.
视图:
$.post('<?php echo site_url('academic/ajax_get_subject_credit'); ?>', {'subjectid':subjectid}, function(data){
$('#chours' + id).val(data); });
Run Code Online (Sandbox Code Playgroud)
这从下拉列表获取值,我想从下拉列表中自动填充文本字段.#chours
是文本字段ID.
控制器:
function ajax_get_subject_credit($result)
{
$this->db->select('subjectid, subjectcredit');
$query = $this->db->get('ref_subject');
$result = $query->result_array();
$query->free_result();
$subjectid = array();
foreach($result as $row)
{
$result = $result + array($row['subjectid'] => $row['subjectcredit']);
}
return $result;
}
Run Code Online (Sandbox Code Playgroud)
在控制器中修改
我也尝试在控制器中使用这个语句直接调用该字段但仍然没有成功:
function ajax_get_subject_credit($subjectid)
{
$this->db->select('subjectid, subjectcredit');
$this->db->where('subjectid',$subjectid);
$query = $this->db->get('ref_subject');
$credithour = $query->row()->subjectcredit;
$query->free_result();
echo $credithour;
}
Run Code Online (Sandbox Code Playgroud)
我将在这里提供一个一般的例子
在视图文件中
$.post('<?php echo site_url("test/test"); ?>', {'id':1}, function(response){
if(response.success)
{
alert(response.message);
} else
{
alert('Something went wrong!!');
}
}, 'json');
Run Code Online (Sandbox Code Playgroud)
在控制器Test.php中
function test()
{
$id = $this->input->post('id');
//do additional stuff
$result = 'i am coming right out of controller!! ';
echo json_encode(array('success' => true, 'message' => $result));
}
Run Code Online (Sandbox Code Playgroud)