我创建了州和城市的相关下拉列表。这是属性提交表单。当用户编辑表单时,我想显示所选的州和城市。我设法显示了选定的状态。我已经编写了get_city函数function.php,并通过ajax调用它。我有两张不同的桌子,即州和市。所以问题是我不明白如何显示选定的城市??这是我的下拉代码
<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>
在上面的代码中,我显示所选状态。我在谷歌上搜索,发现我必须将post-id传递给function.php中的函数。这是我在function.php文件中为get_city函数编写的代码
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代码。。。
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;?>">
您必须将上述代码放入函数中。让函数名称为myFunction()
。如果要将post-id传递给函数,请调用以post-id为参数的函数,即myFunction($postid)
,其中$postid
是post-id。请不要忘记从post循环调用函数,这样函数将为每个postid调用。
我亲自为每个状态加载一个,使用style="display:none
试试这个,告诉我它是否有效。在global$edit_id;已删除,$property_id行已更改。请正确应用更改,并让我知道它们是否可以。
<?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");