i’m using code igniter and I made a form with the code igniter form helper and I load 2 dropdown boxes with data from my Database and that works perfectly. And when I fill all fields in it works. I have currently 2 required fields. And 1 of them is not required. When I fill in all the required fields, it works, but from when I don’t fill in one required field. It gives this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: Cities
Filename: admin/adddivelocation_view.php
Line Number: 67
The code on line number 67 is:
echo form_dropdown('duikplaats', $Cities, '', $attributes_dropdown_gemeente);
I get the $cities in the view like this:
function addDiveLocation()
{
$data['Cities'] = $this->admin_model->getCity();
$data['Countries'] = $this->admin_model->getCountries();
$this->is_logged_in('admin/adddivelocation_view', $data);
}
My script where the values or the attributes of the 2 drop down menus change are:
$('#duikplaats').change(function(){
var data = {
'id': $(this).val()
}
$.get(CI.base_url + "/admin/changeCountry", data, function(content){
console.log(content);
$('#duikland option:selected').removeAttr('selected');
$('#duikland option').filter(function(){
return $(this).val() == content;
}).attr('selected', true);
});
});
//Wanneer Land gekozen word
$('#duikland').change(function(){
var data = {
'id': $(this).val()
}
$.get(CI.base_url + '/admin/changeCity', data, function(content){
$('#duikplaats').empty();
var landen = $.parseJSON(content);
$.each(landen, function(k, v){
$('#duikplaats').append(
'<option value="' + k + '">'+ v + '</option>'
);
});
});
});
and the 2 functies that the jQuery function calls:
changeCountry:
function changeCountry(){
$query = $this->admin_model->getCountry($this->input->get('id'));
$this->output->set_output($query->FK_LandID);
}
changeCity:
function changeCity(){
$query = $this->admin_model->getCities($this->input->get('id'));
foreach($query as $object)
{
$data[$object->id] = $object->Gemeente;
}
$data = json_encode($data);
$this->output->set_output($data);
}
I’ve found the solution. When I post my form it goes trough a validation function, there I load the success view when it is successfully posted or the original view when it failed. When it failed it only loaded the view but I forgot to give any data back to the view so it couldn’t load the data.
Thanks anyway!