javascript - 如何在编辑页面上显示数据库中的州和城市名称?
问题描述
我正在从编辑页面的数据库中获取数据。
我正在使用 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
解决方案
在编辑视图中,您需要执行与国家/地区相同的操作。首先,您需要从 db 中获取您所有的州和城市。
如果您在创建过程中不存储 state_id 和 city_id ,还需要将其存储在 db 中。
现在您需要将这些数据传递到您的编辑视图,并像为国家/地区所做的那样做同样的事情:-
<select name = "state">
<option value="">Choose State</option>
<?php foreach($state as $row){ ?>
<option value="<?php echo $row->state_id; ?>" <?php if($row->state_id == $selected->state_id){ echo "SELECTED";} ?>> <?php echo $row->state_id; ?></option>
<?php } ?>
</select>
对城市也做同样的事情。还请记住,如果用户更改城市,您也需要在编辑页面执行 ajax。
这应该是控制器上的代码:-
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); //get here your saved state id of this user and match it in the view level
$got_customer_info['get_country']=$this->Customer_model->get_country();// all country name;
$got_customer_info['get_state']=$this->State_model->get_state();// all state name;
$this->load->view('customer/search_order',$got_customer_info);
}
推荐阅读
- c# - 仅使用身份服务器 4 中的电话号码创建帐户
- javascript - 防止用户更改 url 参数?
- linux - 在 Ansible 中使用 cacheable 时:set_facts 中的 true,如何在通过 playbook 执行后清除特定的事实
- java - 如何在嵌套字段未完全填充的情况下使用示例匹配器
- java - browserstack-cypress init 命令不起作用
- openlayers-3 - Openlayer 地图不可见我已成功安装 npm
- spring-boot - 找不到有效的 Docker 环境。请查看日志并检查配置
- android - 未找到处理 Intent 的 Activity (android.intent.action.VIEW)
- python - 如何在 Python 中为标签抓取确切的类名?
- kframework - 在 Map 中查找以获取存储变量的值的问题