How to get a row from database using ajax in codei

2019-09-02 04:59发布

问题:

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.

回答1:

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.



回答2:

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

The $data which you are echoing in the controller is basically an array[] and programs is an array which is present in $data. Either echo the $data in controller using the

      foreach(){}

or echo the $query array in your model. That will do the trick.And in ajax success call just append the data to the element in which you want to show the result.



回答3:

Change names as per your page.

in your script:

$.ajax({
    url: '<?php echo base_url();?>managealerts_edit/editalerts',
    type: "POST",
    data: {'id': edit_id},
    cache: false,
    dataType: "json",
    success: function(row){ 
        //alert(row.sub);
        $('#edit').show();
        $('#sub').val(row.sub);
        $('#mess').val(row.mess);
    }
});

in your model:

$query = $this->db->query("SELECT fld_id, fld_course_id,fld_sub,fld_mess from tbl_alerts where fld_id='".$det."' ");
if ($query->num_rows() > 0)
{
    $row = $query->row_array(); 
    $data=array("sub" => $row['fld_sub'], "mess" => $row['fld_mess']);
    echo json_encode($data);
}

in your controller:

$det = $this->input->post('id');
//$alertsres['tbl_alerts'] = $this->managealerts_m->select_editalerts($det);
$this->managealerts_m->select_editalerts($det);`