问题描述
我创建了州和城市的依赖下拉列表.是财产申报书.用户编辑表单时,我想显示所选的州和城市.我设法以某种方式显示选定的状态.我写了get_city函数function.php并通过ajax调用它,我有两个不同的表,即州和城市.所以问题是我不明白如何显示所选城市?这是我的下拉代码
I have created dependent drop down of state and city. It is property submission form. When user edit the form, I want to display selected state and city. I managed to display selected state somehow. I have written get_city function function.php and call it through ajax.I have two different tables i.e. state and city. So problem is I dont understand how to show selected city??This is my code for drop down
<select name="state" id="state" class="select-submit2">
<option value="">Select state</option>
<?php
$property_id = $_GET['listing_edit'];
$property_state = get_post_meta($property_id, state, true);
$result=$wpdb->get_results("select * from states");
//$wpdb->get_results($query);
foreach($result as $row) {
$state_id=$row->state_id;
$state_name=$row->state_name;
?>
<option value="<?php echo $state_id; ?>" <?php if($property_state == $state_id){ echo
'selected="selected"';} ?>><?php echo $state_name; ?></option>
<?php
//echo '<option value='.$state_id.'>'.$state_name.'</option>';
}
?>
</select>
<input type="hidden" name="edit_id" id="edit_id" value="<?php echo $edit_id;?>">
<select name="district" id="district" required class="select-submit2">
<option value="">Select District</option>
</select>
在上面的代码中,我正在显示选定状态.我在Google上进行搜索,发现我必须将post ID传递给function.php中的函数.这是我对function.php文件中的get_city函数的代码
In the above code i am displaying selected state. I searched on google and found that I have to pass post id to function in function.php. This is my code for get_city function in function.php file
function getcity() {
global $wpdb;
if($_POST['state']) {
$id = $_POST['state'];
global $edit_id;
$property_id = $edit_id;
$district = get_post_meta($property_id, 'district', true);
$result=$wpdb->get_results("SELECT * FROM districts WHERE state_id='$id'");
foreach($result as $row) {
$city_name = $row->district_name;
$city_id = $row->district_id;
?>
<option value="<?php echo $city_id; ?>" <?php if($district == $city_id){ echo 'selected="selected"';} ?>><?php echo $city_name; ?></option>
<?php
//echo '<option value="'.$city_id.'">'.$city_name.'</option>';
}
}
}
add_action("wp_ajax_nopriv_getcity", "getcity");
add_action("wp_ajax_getcity", "getcity");
这是我的js代码...
This is my js code...
jQuery(document).ready(function($) {
$('#state').on('change', function() {
var state = $('#state').val();
var edit_id = $('#edit_id').val();
$.ajax
({
type : "POST",
url : "http://whitecode.in/demo/plotsup_plot/wp-admin/admin-ajax.php",
data :{'action' : 'getcity', 'state' : state , 'edit_id' : edit_id },
success: function(html) {
$("#district").removeAttr("disabled");
$("#district").html(html);
}
});
});
<input type="hidden" name="edit_id" id="edit_id" value="<?php echo $edit_id;?>">
推荐答案
尝试一下,让我知道它是否有效.在全局$ edit_id的两行中进行了更改;已删除,并且$ property_id行已更改.请正确应用更改,让我知道是否可以.
Try this and let me know if it works or not. The change is made on two lines global $edit_id; has been removed and the $property_id line has been changes. Please apply the changes properly and let me know if they are ok.
<?php
function getcity() {
global $wpdb;
if($_POST['state']) {
$id = $_POST['state'];
$property_id = $_POST['edit_id'];
$district = get_post_meta($property_id, 'district', true);
$result=$wpdb->get_results("SELECT * FROM districts WHERE state_id='$id'");
foreach($result as $row) {
$city_name = $row->district_name;
$city_id = $row->district_id;
?>
<option value="<?php echo $city_id; ?>" <?php if($district == $city_id){ echo 'selected="selected"';} ?>><?php echo $city_name; ?></option>
<?php
//echo '<option value="'.$city_id.'">'.$city_name.'</option>';
}
}
}
add_action("wp_ajax_nopriv_getcity", "getcity");
add_action("wp_ajax_getcity", "getcity");
这篇关于用户编辑任何表单时如何显示所选的下拉值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!