我是wordpress开发的新手,我有一个包含zipcode值和学校名称的表(表中可能有数千个结果)。我要做的是从用户检索zipcode,并根据zipcode值在表中搜索它,然后根据搜索结果显示与zipcode用户输入的zipcode相关的学校名称。到目前为止,我能够获得zipcode并通过以下方式进行搜索:(这是我现在拥有的经过编辑的部分和最新的代码)
<div class="col-xs-4">
<div class="zipcode">Zip Codes
<input type="text" class="form-control postcode" name="postalCode" />
<input type="button" id="findschoolname">
</div>
<div id="replacedropwdown">
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Institutions
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#"</a></li>
</ul>
</div>
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#findschoolname").click(function(){
var postcode = jQuery(\'.postcode\').val();
var postData = {
action: \'finescroolname\',
postcode: postcode,
}
var adminurl = admin_url( \'admin-ajax.php\' );
jQuery.post(ajaxurl, postData, function(response) {
jQuery(\'#replacedropwdown\').html(response);
});
});
});
以及我的职能。php是:
add_action( \'wp_ajax_finescroolname\', \'finescroolname\' );
function finescroolname(){
global $wpdb;
$postcode = $_POST[\'postcode\'];
$school_query = "SELECT DISTINCT schoolname from wp_db where zipcode=\'.$postcode.\'";
$results = $wpdb->get_results($school_query);
$html = \'drop down html\';
echo $html;
wp_die();
}
所以我的问题是关于这一部分,我如何才能将这些结果传递到wordpress的下拉列表中?它将与ajax合作吗?有人能帮我写一小段代码,让我得出结果吗?感谢您的时间和关注。因此,通过@user3888958帮助,我可以在ajax帖子中看到结果:
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#findschoolname").click(function(){
var postcode = jQuery(\'.postcode\').val();
var postData = {
action: \'finescroolname\',
postcode: postcode,
}
var adminurl = admin_url( \'admin-ajax.php\' );
jQuery.post(ajaxurl, postData, function(response) {
jQuery(\'#replacedropwdown\').html(response);
});
});
});
</script>
但当我尝试在函数中调试finesColName()函数时,它仍然没有将postcode的值传递到下拉列表中。php我在那里看不到任何邮政编码。
SO网友:Bhagchandani
在以下代码中,当您按下id为的按钮时findschoolname
ajax将运行。您可以通过更改文本框来更改单击事件。下面的脚本运行ajax。
<div class="col-xs-4">
<div class="zipcode">Zip Codes
<input type="text" class="form-control postcode" name="postalCode" />
<input type="button" id="findschoolname">
</div>
<div id="replacedropwdown">
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#findschoolname").click(function(){
var postcode = jQuery(\'.postcode\').val();
var postData = {
action: \'finescroolname\',
postcode: postcode,
}
var adminurl = admin_url( \'admin-ajax.php\' );
jQuery.post(ajaxurl, postData, function(response) {
jQuery(\'#replacedropwdown\').html(response);
});
});
});
</script>
将以下代码置于功能中。php:
add_action( \'wp_ajax_finescroolname\', \'finescroolname\' );
function finescroolname(){
global $wpdb;
$postcode = intval( $_POST[\'postcode\'] );
$school_query = "SELECT DISTINCT schoolname from wp_db where zipcode=\'.$postcode.\'";
$results = $wpdb->get_results($school_query);
// Create html here and echo it;
$html = \'drop down html\';
echo $html;
wp_die();
}
希望这会有点帮助。