Reputation: 63
I'm trying to get data from my database to my view using ajax in codeigniter, but I can't get it working. I think something is going wrong with passing the grade_id to my model function.
AJAX:
<script type="text/javascript">
$(document).ready(function(){
$('#grades').change(function(){
$("#classes > option").remove();
var grade_id = $('#grades').val();
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>index.php/login/get_classes/"+grade_id,
data: {gradeid:grade_id},
success: function(classes)
{
$.each(classes,function(id,name)
{
var opt = $('<option />');
opt.val(id);
opt.text(name);
$('#classes').append(opt);
});
}
});
});
});
</script>
CONTROLLER:
function get_classes($gradeid)
{
$this->load->model('school_info_model');
header('Content-type: application/json');
if ($query = $this->school_info_model->get_all_classes($gradeid))
{
echo json_encode($query);
} else {
redirect('login/loginform');
}
}
MODEL:
function get_all_classes($gradeid)
{
$this->db->where('grade_id', $gradeid);
$query = $this->db->get('classes');
if($query->result()){
$result = $query->result();
foreach($result as $row)
{
$options[$row->id] = $row->name;
}
return $options;
}
}
EDIT:
I updated my code it is working now except for 2 things, I keep getting an error.
ERROR:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: helpers/form_helper.php
Line Number: 331
And When I load the page for the first time the input field for classes is empty because it only activates the AJAX if the value of grades changes.
Upvotes: 0
Views: 16292
Reputation: 27
You should define you $option in array.
function get_all_classes($gradeid)
{
$this->db->where('grade_id', $gradeid);
$query = $this->db->get('classes');
$options = array();
if($query->result()){
$result = $query->result();
foreach($result as $row)
{
$options[$row->id] = $row->name;
}
return $options;
}
}
Upvotes: 0
Reputation: 521
If you do not get a result body in the first place, then json_encode
most likely is just returning FALSE.
Just to point out, you have a comparison error in the first if-clause of your model's function
if($gradeid = NULL){
$this->db->where('grade_id', $gradeid);
}
Edit... wait, mhm, your assignment-testing style is confusing me. I mean, this where clause is never executed.
further you might consider returning the empty $options
list if no classes have been found, also in your controller, as in theory (without knowing your application) that would seem to make sense.
Telling your AJAX call explicitly that it is expecting JSON data might not be necessary, but might help, ie add dataType: "JSON"
as a setting.
Edit in response to your form error:
The error you are getting is a standard php error which basically says that the foreach loop gets a wrong argument.. likely you do not pass it an array. For that you must check login/loginform
respectively its view.
But I am not sure what your intention is there? To forward the user to the login form if he/she/it does not receive the expected data? Do you deal with that in your AJAX request? As it is now, your AJAX request would just get rerouted to something which likely is not valid JSON, but the browser's location does not change.
Upvotes: 1
Reputation: 915
Instead of passing it as a parameter to your controller, try serializing the value and use this in ur script.
var grade_id = $("#grades").serialize();
and
data: grade_id
And in your controller,
$grade_id = $this->input->post('grade_id');
This should work!
Upvotes: 1
Reputation: 1924
I would set my controller up like this:
function get_classes()
{
$this->load->model('school_info_model');
if ($gradeid = $this->input->post('gradeid')
{
$query = $this->school_info_model->get_all_classes($gradeid);
header('Content-Type: application/json');
echo json_encode($query);
} else {
redirect('some/controller');
}
}
And I would change my JS to work like this:
<script type="text/javascript">// <![CDATA[
$(document).ready(function(){
$('#grades').change(function(){
$("#classes > option").remove();
var grade_id = $('#grades').val();
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>index.php/login/get_classes/",
data: {gradeid:grade_id},
success: function(classes)
{
$.each(classes,function(id,name)
{
var opt = $('<option />');
opt.val(id);
opt.text(name);
$('#classes').append(opt);
});
}
});
});
});
// ]]>
</script>
I'm not sure if that's exactly where you want to be, but it should get you closer.
Upvotes: 1