我需要您的帮助,因为我想根据文本框中的标准从 wordpress 表中获取一些数据。该数据存储在一个数组中,我希望将该数据复制到带有 ajax 和 jquery 的选择框。数据不会被复制到选择列表选项中。我想将数组发送到表单提交或按搜索到使用 for 循环将该数组结果分配给简单选择框中的选项的 ajax。下面是我的代码。谢谢大家
html代码
<SELECT id=”myselect”></SELECT>
php函数
add_action( 'wp_ajax_wp_hello', 'wp_hello' );
add_action( 'wp_ajax_nopriv_wp_hello', 'wp_hello');
function wp_hello()
{
$secretcode=$_POST['secretcode'];
//main logic
global $wpdb;
//echo jsonencode($secretcode);
$sql = "SELECT DISTINCT * FROM wp_store_locator WHERE sl_store LIKE '$secretcode%%' ";
$results = $wpdb->get_results($sql) or die(mysql_error());
$takeit = array();
foreach( $results as $result ) {
echo $takeit[].= $result->sl_store;
}
exit();
}
jQuery代码
jQuery(function ($) {
//$("#myform").submit(function (e) { //form is intercepted
// e.preventDefault();
$("#secret").keyup(function(){
var a = $("#secret").val();
//serialize the form which contains secretcode
var sentdata = $(this).serializeArray();
//Add the additional param to the data
sentdata.push({
name: 'action',
value: 'wp_hello'
})
//set sentdata as the data to be sent
$.post(yes.ajaxurl, sentdata, function (rez) { //start of funciton
$("#result").append(rez);
for (var i=0; i<rez.length; i++)
{
$("#myselect").append('<option value="' + rez.eq(i) + '">' + rez[i]+rez+ '</option>');
}
return false;
} //end of function
,
'html'); //set the dataType as json, so you will get the parsed data in the callback
});