I want to have three drop downs of countries, states, and cities and I want to populate them from a database. I am successful in retreving values for the countries drop down but the problem is the state drop down where values are shown according to selected country. As an example, if I select U.S.A I only want to get states in the U.S. I have 2 tables countries and states and I have country_id as foreign key in state table actually this what my problem is. My code:
my Controller
//call to fill the second dropdown with the states
public function buildDropStates()
{
//set selected country id from POST
echo $id_country = $this->input->post('country_id');
//var_dump($id_country);
//run the query for the cities we specified earlier
$districtData['districtDrop']=$this->Country_states_cities->getCityByCountry($id_country);
//var_dump($districtData);
$output = null;
foreach ($districtData['districtDrop'] as $row)
{
//here we build a dropdown item line for each query result
$output .= "<option value='".$row->state_id."'>".$row->state_name."</option>";
}
echo $output;
// var_dump($output);
}
My model
//fill your state dropdown depending on the selected country
public function getCityByCountry($country_id=string)
{
$this->db->select('state_id,state_name');
$this->db->from('nm_state');
$this->db->where('country_id',$country_id);
$query = $this->db->get();
//var_dump($query);
var_dump($query->result());
return $query->result();
}
}
and This view
<script>
$(document).ready(function() {
$("#country").change(function(){
/*dropdown post *///
$.ajax({
url:"<?php echo
base_url();?>Country_state/buildDropStates",
data: {id:
$(this).val()},
type: "POST",
success:function(data){
$("#state").html(data);
}
});
});
});
</script>
<label for="country">Country</label>
<?php echo form_dropdown('country',$countryDrop,'','class="required" id="country" '); ?>
<br />
<br />
<label for="state">State</label>
<select name="state" id="state">
<option value="">Select</option>
</select>
The problem is that my model getCityByCountry() is returning an empty array with a size of 0. When I comment the following line out of my model it returns all the values from DB so what is actually happening here? Is the foreign key Constraint not working or is it something else??
$this->db->where('country_id',$country_id);
on echoing query
CI_DB_mysqli_result Object ( [conn_id] => mysqli Object ( [affected_rows] => 0 [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: 3c688b6bbc30d36af3ac34fdd4b7b5b787fe5555 $ [client_version] => 50011 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 2 [host_info] => localhost via TCP/IP [info] => [insert_id] => 0 [server_info] => 5.6.24 [server_version] => 50624 [stat] => Uptime: 2694 Threads: 1 Questions: 6365 Slow queries: 0 Opens: 88 Flush tables: 1 Open tables: 81 Queries per second avg: 2.362 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 715 [warning_count] => 0 ) [result_id] => mysqli_result Object ( [current_field] => 0 [field_count] => 2 [lengths] => [num_rows] => 0 [type] => 0 ) [result_array] => Array ( ) [result_object] => Array ( ) [custom_result_object] => Array ( ) [current_row] => 0 [num_rows] => [row_data] => )
Ajax.Post
.. create an event which isonchange
.. then do aselect
query based on the value selected.... – Sam Teng Wong Sep 1 '15 at 1:53execute()
code somewhere else? – Sam Teng Wong Sep 1 '15 at 2:01