1

I need some help with the following code. I am trying to query my database using ajax and return the results within a table. The code is executing and I am not getting any errors - I'm just not getting a response. Any ideas? I have a feeling something is messed up with my jQuery function.

Thanks for any help you can give.

My function in my view:

$("#test").click(function() {
    var form_data = {
        Opportunity_Id: $('#Opportunity_Id').val(),
        ajax: '1'
    };

    $.ajax({
        url: "<?php echo site_url('schedule/get_classes'); ?>",
        type: 'POST',
        data: form_data,
        cache: false,
        success: function(server_response) {
            var data = $.parseJSON(server_response);

            for(var i = 0; i < data.length; i++){
                week = data[i];
                $("table.table").append("<tr><td>" + week.Class_Number + "</td><td>" + week.Class_Date + "</td></tr>");
            };
        },
        error: function(thrownError) {
            alert(thrownError);
        }
    });

    return false;
})

My controller:

function get_classes() {
    $Opportunity_Id = $this->input->post('Opportunity_Id');
    $this->ion_auth_model->get_classes($Opportunity_Id);

    foreach ($classes as $class):
        $results[] = array(
                        "Class_Number" => $class->Class_Number,
                        "Class_Date" => $class->Class_Date,
                        "Start_Time" => $class->Start_Time,
                        "End_Time" => $class->End_Time
                    );
    endforeach;

    echo json_encode($results);
}

My model:

function get_classes($Opportunity_Id) {
    $this->db->select('*')->from('Classes')->where('Opportunity_Id', $Opportunity_Id);
    $q = $this->db->get();

    if($q->num_rows() > 0) {
        foreach($q->result() as $classes) {
            $data[] = $classes;
        }
        return $data;
    }
}
6
  • How is your data variable looks like in ajax response? Commented Sep 4, 2014 at 16:35
  • try firebug or chrome debugger to check what response you get or error you receive. Commented Sep 4, 2014 at 16:41
  • @RonakPatel I'm not sure I understand. I see the code is executing by looking at the network log within my browser dev tools. Within my view, the rows are not populating within my table. Commented Sep 4, 2014 at 16:42
  • @Zeeshan I am getting the following error in the console - "Uncaught SyntaxError: Unexpected token <" Commented Sep 4, 2014 at 16:45
  • @user1337595 at which line are you getting this syntax error? and why don't you mention it in your question? Commented Sep 4, 2014 at 16:54

1 Answer 1

1

I think this maybe your problem, you are returning an array from get_classes($Opportunity_Id) but you have not captured that array when you call it :-

function get_classes() {
    $Opportunity_Id = $this->input->post('Opportunity_Id');
    $classes = $this->ion_auth_model->get_classes($Opportunity_Id);   <--- changed here

    foreach ($classes as $class):
        $results[] = array(
                        "Class_Number" => $class->Class_Number,
                        "Class_Date" => $class->Class_Date,
                        "Start_Time" => $class->Start_Time,
                        "End_Time" => $class->End_Time
                    );
    endforeach;

    echo json_encode($results);
}
Sign up to request clarification or add additional context in comments.

Comments

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.