4

I am trying to pass a date into an ajax function and get the relevant type and title according to that date separately from the database.

I have used the following Model function.

function get_reservation_for_add_popup($date_cal) {
        $this->db->select('reservations.title,reservations.type');
        $this->db->from('reservations');
        $this->db->where('reservations.date_cal', $date_cal);
        $this->db->where('reservations.is_deleted', '0');
        $query = $this->db->get();
        return $query->result();      
    }

My Controller function is like following

function get_title_and_type() {
        $reservation_service = new Reservation_service();        
        $data['reservation_pop_up'] = $reservation_service->get_reservation_for_add_popup($this->input->post('date', TRUE));    

        echo $data;
    }

In the View I want to get title and the type separately for the particular day. I used the following ajax function. But i don't know how to return values from an ajax function and how to catch them into variables.

var date = $('#date').val();
                var title=[];
                var type=[];

                $.ajax({
                    type: 'POST',
                    url: '<?php echo site_url(); ?>/dashboard/get_title_and_type',
                    data: 'date=' + date,
                    success: function(msg) {
                        return msg;                        
                    }
                });

Since i am very new to ajax it will be a great help if someone give me an idea.

4
  • ajax is ASYNC so you don't return values from an ajax function, you write your program in the callback function. Write console.log(msg) instead of return msg and you would see the data you queried from database Commented Jul 8, 2015 at 8:59
  • See my answer ... and use GET method instead of POST Commented Jul 8, 2015 at 9:03
  • Actually your question is misleading title please elaborate what you need and what's not working over here Commented Jul 8, 2015 at 9:04
  • I mentioned the header directive in a comment in one of the answers and the use of the shorthand $.post()... Renders the use of $.parseJSON(msg) useless when done correctly... ;) Commented Jul 8, 2015 at 9:06

3 Answers 3

3

In ajax Change following

data: 'date=' + date,

TO

data: {'date' : date},
Sign up to request clarification or add additional context in comments.

4 Comments

@Sadikhasan this won't make any difference
@Uchiha If you don't then it give syntactically error.
@Sadikhasan now that you have edited your answer I look like an idiot. Previously it was data: 'date' : date Nevermind, as long it helps the OP
@SudhirMishra You can point out my mistake but you ask to me how would this work ?
1

Send any kind of data JSON encoded from your PHP file

function get_title_and_type() {

        ..... your code here
        $data['test_msg_2'] = "Hello 1";
        $data['test_msg_2'] = "Hello 2";
        echo json_encode($data);
}

And in your JS

$.ajax({
     type: 'GET',
     url: '<?php echo site_url(); ?>/dashboard/get_title_and_type',
     data: 'date=' + date,
     success: function(msg) {
         msg = $.parseJSON(msg);
         console.log(msg.reservation_pop_up);   
         console.log(msg.test_msg_2);                      
         console.log(msg.test_msg_2);                   
     }

Comments

1

In controller

function get_title_and_type() {
        $reservation_service = new Reservation_service();        
        $data['reservation_pop_up'] = $reservation_service->get_reservation_for_add_popup($this->input->post('date', TRUE));    

        echo json_encode($data);
    }

In your ajax add dataType json

var date = $('#date').val();
                var title=[];
                var type=[];

                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: '<?php echo site_url(); ?>/dashboard/get_title_and_type',
                    data: {'date' : date},
                    success: function(msg) {
                        return msg;                        
                    }
                });

2 Comments

I would add header('Content-type: application/json'); to the controller action to force detection of JSON data being sent... Since jquery will detect that header, it will try to create a JSON object as response result in your success callback. I would also encourage the use of the shorthand $.post(url, { ... data ...}, callback(jsonResponse) { ... code ... });
Yes that's also fine.

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.