Skip to content Skip to sidebar Skip to footer

How To Get A Row From Database Using Ajax In Codeigniter

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

Solution 1:

$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.

Solution 2:

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);`

Solution 3:

in model

functionget_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.

Post a Comment for "How To Get A Row From Database Using Ajax In Codeigniter"