如何创建一个动态相关的选择框,它可以从数据库中提取记录,而无需使用ajax重新加载页面。
这是我的密码。阿贾克斯城市。php
<?php
add_action( \'wp_enqueue_scripts\', \'my_enqueue\' );
function my_enqueue() {
wp_enqueue_script( \'ajax-script\', plugins_url( \'/cities.js\', __FILE__ ), array(\'jquery\'));
wp_localize_script( \'ajax-script\', \'ajax_object\', array( \'ajax_url\' => admin_url( \'admin-ajax.php\' )));
}
function my_action() {
$country_id = $_REQUEST[\'id\'];
global $wpdb;
$cities = $wpdb->get_results($wpdb->prepare("SELECT * FROM city WHERE country_id=\'".$country_id."\'",13,\'gargle\'),ARRAY_A);
foreach($cities as $city)
{ ?>
<option value="<?php echo $city["city_id"]; ?>"><?php echo $city["city_name"]; ?></option>
<?php }
die();
}
?>
页面选择国家/地区。php
<?php
get_header(); ?>
<div id="contents">
<h1>Select Country</h1><br/><br/>
<form name="form" method="post">
<label>Country: </label><select name="country" class="country">
<option>Select Country</option>
<?php
global $wpdb;
$countries = $wpdb->get_results($wpdb->prepare("SELECT * FROM country",13,\'gargle\'),ARRAY_A);
foreach($countries as $country)
{ ?>
<option value="<?php echo $country["country_id"]; ?>"><?php echo $country["country_name"]; ?></option>
<?php } ?>
</select>
<label>City: </label><select name="city" class="city">
<option>Select City</option>
</select>
</form>
</div>
<?php get_footer(); ?>
但不显示从属城市名称。