如何使用codeigniter中的ajax从数据库中获取一行 [英] How to get a row from database using ajax in codeigniter

查看:82
本文介绍了如何使用codeigniter中的ajax从数据库中获取一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用codeigniter中的ajax从数据库中获取一行数据。



这里是javascript函数 -

  $(function(){
$(button [name ='program_view_details'] .preventDefault();
var program_id = $(this).attr('id');
$ .ajax({
url:<?php echo base_url > program_management / get_program_data,
type:POST,
dataType:html,

data:program_id =+ program_id,
success: function(row)
{
alert(row.program_name);
}
});


});

我不确定数据类型和帖子是否正确。



这是我的控制器函数 -

  public function get_program_data(){
$ program_id = $ this - > input-> post('program_id');
$ this-> load-> model('program_management_model');
$ data ['programs'] = $ this-> ; program_management_model-> get_program_specific($ program_id);
echo $ data

}

以下是模型 -

  function get_program_specific($ program_id){
$ query = $ this-> db-> query(SELECT * FROM programs WHERE program_id = '。$ program_id。');
return $ query-> result();
}

我正在搜索从控制器返回到javascript的行的方式。但是alert()在成功中显示未定义。请任何人告诉我整个过程。提前感谢。

 

code> function get_program_specific($ program_id){
$ temp = array();
$ query = $ this-> db-> query(SELECT * FROM programs WHERE program_id ='。$ program_id。');
$ temp = $ query-> row_array();
echo $ temp ['program_name'];
}

在控制器中更改行

  $ data ['programs'] = $ this-> program_management_model-> get_program_specific($ program_id); 

  $ this-> program_management_model-> get_program_specific($ program_id); 

,最后在javascript中

  alert(row); 

如果您遇到任何问题,请通知我们。


i am trying to get a row of data from database using ajax in codeigniter.

Here is the javascript function-

$(function(){
    $("button[name='program_view_details']").click(function(e){
    e.preventDefault();
    var program_id=$(this).attr('id');
    $.ajax({
        url: "<?php echo base_url();?>program_management/get_program_data",
        type: "POST",
        dataType: "html",

        data: "program_id="+program_id,
        success: function(row)
        {
            alert(row.program_name);
        }
    });


});

I am not sure if the datatype and post is correct or not.

Here is my controller function-

public function get_program_data( ){
    $program_id = $this->input->post('program_id');
    $this->load->model('program_management_model');
    $data['programs']= $this->program_management_model->get_program_specific($program_id);
    echo $data;

}

Here is the model-

function get_program_specific($program_id){
    $query=$this->db->query("SELECT * FROM programs WHERE program_id='".$program_id."'");
    return $query->result();
}

I am searching the way of returning the row from controller to javascript. But the alert() is showing "undefined" in the success. Please anyone tell me the whole way through. Thanks in advance.

解决方案

in model

function get_program_specific($program_id){
    $temp=array();
    $query=$this->db->query("SELECT * FROM programs WHERE program_id='".$program_id."'");
    $temp= $query->row_array();
    echo $temp['program_name'];
}

in controller change the line

$data['programs']= $this->program_management_model->get_program_specific($program_id);

with

$this->program_management_model->get_program_specific($program_id);

and finally in javascript

alert(row);

please let me know if you face any problem.

这篇关于如何使用codeigniter中的ajax从数据库中获取一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆