4

my problem is that i can't access the array returned from controller to ajax success function.

here my controller code:

function get_slot()
{
    $fac_id = 1;
    $date = $this->input->post('date');

    $this->load->model('booking_model');
    $result = $this->booking_model->get_slot_availablity($date, $fac_id );  
    $data['booking'] = $result['row'];

    echo json_encode($data);
}

my ajax function:

 $.ajax({
          type : "Post",
           url : "<?php echo base_url(); ?>index.php/index/get_slot",
          data : "date="+date,
      dataType : 'json',
       success : function(result)
                {
                    $.each(result, function(index, val) {
                        alert(val.slot_id); 
                    });
                }
       });

my model function:

public function get_slot_availablity($date, $fac_id){

    $q = $this->db->select('*')
    ->from('booking')
    ->where('f_id', $fac_id)
    ->where('date', $date);

    $res['row'] = $q->get()->result();

    return $res;
}

the function displays undefined

9
  • Check browser console, what is you are getting from server? Commented Jan 20, 2015 at 7:22
  • there is no need to header() first alert(result); in your success block then start parse your json data Commented Jan 20, 2015 at 7:23
  • i already delete header(), and i add dataType json..but still get undefined @RakeshSharma Commented Jan 20, 2015 at 7:36
  • Please list this $this->booking_model->get_slot_availablity() function's code as well. Commented Jan 20, 2015 at 7:44
  • i already edit my question @Mysteryos Commented Jan 20, 2015 at 7:46

1 Answer 1

2

As per your latest comment, your JS code should be as follows:

$.ajax({
      type : "Post",
       url : "<?php echo base_url(); ?>index.php/index/get_slot",
      data : "date="+date,
  dataType : 'json',
   success : function(result)
            {
                $.each(result.booking, function(index, val) {
                    alert(val.slot_id); 
                });
            }
   });
Sign up to request clarification or add additional context in comments.

5 Comments

please could you explain more about your latest comment above?
You are echoing a json encoded $data variable, whereby the $data['booking'] contains your DB result. Translating the array to JSON, booking becomes an attribute of your result object, hence the result.booking.
i got it, i guess its better to return the array from model directly to result object instead of using multiple objects. thank you @Mysteryos
Indeed. Also, i would recommend you step away from CodeIgniter's active record. It may be good for simple stuff, but has no alternative for complex mySQL queries. Go with raw mySQL and bring uniformity to your codebase.
thank you @Mysteryos for the hints and advices, appreciate it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.