这是我的密码functions.php
文件
function getcity(){
global $wpdb;
if($_POST[\'state\']) {
$id=$_POST[\'state\'];
$postid=$_POST[\'post_id\'];
$district = esc_html(get_post_meta($postid , \'district \', true));
$result=$wpdb->get_results("SELECT * FROM districts WHERE state_id=\'$id\'");
foreach($result as $row) {
$district_name=$row->district_name;
$district_id= $row->district_id;
?><option value="<?php echo $district_id; ?>" <?php if($district == $district_id){echo \'selected="selected"\';} ?>>
<?php echo $district_name; ?>
</option><?php
}
}
}
add_action("wp_ajax_nopriv_getcity", "getcity");
add_action("wp_ajax_getcity", "getcity");
这是我的ajax请求代码
$(\'#state\').on(\'change\', function() {
var state = $(\'#state\').val();
var post_id = $(\'#post_id\').val();
$.ajax
({
type : "POST",
url : "http://plotsup.com/plotsup_plot/wp-admin/admin-ajax.php",
data :{\'action\' : \'getcity\', \'state\' : state, \'post_id\': post_id},
success: function(html) {
$("#district").removeAttr("disabled");
$("#district").html(html);
}
});
});
这是模板文件中的“我的代码”
<select name="state" id="state" class="select-submit2" >
<option value="" >Select state</option>
<?php
$property_state = get_post_meta($edit_id, \'state\', true);
$result=$wpdb->get_results("select * from states");
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 }
?>
</select>
<input type="hidden" name="post_id" value="<?php echo $edit_id;?>" id="post_id"/>
我想要此函数中的当前帖子ID。我怎么才能得到呢?
SO网友:Subharanjan
尝试从post值而不是全局$post获取post\\u id。
function getcity(){
global $wpdb;
if($_POST[\'state\']) {
$id=$_POST[\'state\'];
$postid = $_POST[\'post_id\'];
$district = esc_html(get_post_meta($postid , \'district \', true));
$result=$wpdb->get_results("SELECT * FROM districts WHERE state_id=\'$id\'");
foreach($result as $row) {
$district_name=$row->district_name;
$district_id= $row->district_id;
?><option value="<?php echo $district_id; ?>" <?php if($district == $district_id){echo \'selected="selected"\';} ?>>
<?php echo $district_name; ?>
</option><?php
}
}
}
add_action("wp_ajax_nopriv_getcity", "getcity");
add_action("wp_ajax_getcity", "getcity");
<小时>
<div class="container">
<select name="state" id="state" class="select-submit2">
<option value="">Select state</option>
<?php
$property_state = get_post_meta( $edit_id, \'state\', true );
$result = $wpdb->get_results( "select * from states" );
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
}
?>
</select>
<input type="hidden" name="post_id" value="<?php echo $edit_id; ?>" class="post_id" />
</div>
$(\'#state\').on(\'change\', function() {
var state = $(\'#state\').val();
var post_id = $(this).siblings(\'.post_id\').val();
$.ajax
({
type : "POST",
url : "http://plotsup.com/plotsup_plot/wp-admin/admin-ajax.php",
data :{\'action\' : \'getcity\', \'state\' : state, \'post_id\': post_id},
success: function(html) {
$("#district").removeAttr("disabled");
$("#district").html(html);
}
});
});