我需要你的帮助,因为我想根据文本框中的条件从wordpress表中获取一些数据。该数据存储在一个数组中,我希望将这些数据复制到带有ajax和jquery的选择框中。数据未被复制到选择列表选项。我想发送表单提交表单或按搜索到ajax,使用for循环将该数组结果分配给一个简单的选择框中的选项。以下是我的代码。谢谢大家
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
});