Reputation: 175
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;
}
}
Upvotes: 1
Views: 2410
Reputation: 94662
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);
}
Upvotes: 1