【发布时间】:2018-09-15 04:12:57
【问题描述】:
我正在编辑页面中从数据库中获取数据。
我正在使用 CodeIgniter,我有两个视图页面,register 和 edit_register。
我在register 页面上没有任何问题,但我仍在分享这个过程以了解我的问题。在此页面上,国家、州、城市的依赖性也在起作用。我的意思是我选择“印度”然后它将在状态下拉列表中显示所有状态名称。现在我选择“马哈拉施特拉邦”,然后选择“孟买”。完美运行。
我提交了表单并在数据库中,我得到了类似的值
Country_name | state_name | city
101 | 22 | 2707
--------------------------------------
101-India
22-Maharashtra
2707-Mumbai
我们来谈谈edit_register页面。
现在我在edit_register 页面上,我在下拉列表中获取国家名称。我得到了正确的输出“印度”,但没有得到州和城市名称。
所以我的问题是如何从数据库中显示州和城市名称?
当我点击编辑按钮时请检查这个,然后我只得到国家名称但不显示州名。
如果我选择其他国家并再次选择印度,那么我会在下拉列表中获得所有州名。
控制器
它在下拉列表中显示我的编辑页面和国家/地区名称
public function search_with_number(){
$c_mobileno=$this->input->post('c_mobileno_search');
$got_customer_info['search_customer_info']=$this->Customer_model->get_customer_info($c_mobileno);
$got_customer_info['get_country']=$this->Customer_model->get_country();// all country name
$this->load->view('customer/search_order',$got_customer_info);
}
国家/地区下拉菜单可以使用。
<select class="form_control country_change" name="c_s_country" data-target="target_state_dropdown2">
<option value="" disabled selected>Select Country</option>
<?php foreach ($get_country as $row) {?>
<option <?php if($row->id == $post->c_s_country ){ echo 'selected="selected"'; } ?> value="<?php echo $row->id; ?>"><?php echo $row->country_name;?></option>
<?php }?>
</select>
状态下拉菜单不起作用
<select class="form_control state_get" name="c_s_state" id="target_state_dropdown2" data-target="city_dropdown2">
<option value='' disabled selected>Select state</option>
<!--What code I have to user here-->
</select>
城市不工作
<select class="form_control city_get" name="c_s_city" id="city_dropdown2">
<option value="" disabled selected>Select city </option>
<!--What code I have to user here-->
</select>
我在注册页面中使用的 Ajax
/*Get all the state name using country code*/
$(".country_change").on('change',function(){
var country_id=$(this).val();
var select_list = $(this).data('target'); // The dropdown ID
$.ajax({
url:baseUrl +"/Customer_control/statename",
method:"POST",
data:"country_id="+country_id,
dataType: "json",
success:function(data){
$('#'+select_list).empty();
var placeholder="<option value='' disabled selected>Select state</option>";
$('#'+select_list).html(placeholder);
$.each(data, function(i, data) {
$('#'+select_list).append("<option value='" + data.id + "'>" + data.state_name + "</option>");
});
}
});
});
/*Get all the city name using state code*/
$(".state_get").on('change',function(){
var state_id=$(this).val();
var select_list = $(this).data('target'); // The dropdown ID
$.ajax({
url:baseUrl +"/Customer_control/cityname",
method:"POST",
data:"state_id="+state_id,
dataType: "json",
success:function(data){
$('#'+select_list).empty();
var placeholder="<option value='' disabled selected>Select city</option><option value='Other'>Other</option>";
$('#'+select_list).html(placeholder);
$.each(data, function(i, data) {
$('#'+select_list).append("<option value='" + data.id + "'>" + data.cities_name + "</option>");
});
}
});
});
状态模型
public function get_country()
{
$get_country = array('is_country_active' => 1);
$this->db->where($get_country);
$query = $this->db->get('countries');
$result = $query->result();
if($result)
{
return $result;
}
else
{
return 0;
}
}
public function get_state($country_id){
$get_state = array('is_state_active' => 1,'country_id'=>$country_id);
$this->db->where($get_state);
$query = $this->db->get('states');
$result = $query->result();
if($result)
{
return $result;
}
else
{
return 0;
}
}
public function get_city($state_id){
$get_city = array('is_city_active' => 1,'state_id'=>$state_id);
$this->db->where($get_city);
$query = $this->db->get('cities');
$result = $query->result();
if($result)
{
return $result;
}
else
{
return 0;
}
}
我还没有添加城市JS。
现在请检查我的 Ajax 代码,当我选择国家名称时,我的州名正在改变。我必须显示来自数据库的州和城市名称 onload
【问题讨论】:
-
您的
$this->Customer_model->get_country();是否返回用户在注册时发布的州和城市? -
@Nik,感谢您的回复,是的,注册时我得到了所有国家/地区的名称。仅返回国家/地区名称
-
好的。那么您是否错过了选择用户的首选城市和州?您在哪里获取注册用户的城市和州?就像您有
Customer_model->get_country();来获取国家/地区一样吗? -
您的意思是在初始页面加载时不显示状态,还是在更改国家/地区时不显示?
-
我不明白为什么您不能使用与国家相同的代码来获取州(当然选择不同)。只需从数据库中获取状态。如果我正确理解了您的代码,您已经将州名存储在数据库中。
标签: javascript php jquery html codeigniter-3