问题
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>
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");
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;?>">
回答1:
You must put the above code in a function. Let the function name be myFunction()
.
If you want to pass the post id to the function, call the function with post id as its argument, ie, myFunction($postid)
where $postid
is the post id.
Please do not forget to call function from the post loop so that function will call for every postid.
回答2:
I personally load a for each state, with style="display:none;", afterwards, when user selects state the appropriate changes style="display:none;" to style="display:inline;"
回答3:
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");
来源:https://stackoverflow.com/questions/25800727/how-to-display-selected-dropdown-value-when-user-edit-any-form