我正在尝试构建一个具有自动完成功能的Web表单。用户可以填写个人代码,相关字段将显示在表单字段中。
如何将结果提取到表单字段中?
到目前为止我所做的尝试:
在Javascript中查找代码使用AJAX将代码发送到PHP当我使用控制台并在lookup\\u code()函数中检查var\\u dump时,我看到了所需用户的数组这是我的代码:
add_action( \'wp_head\', \'ajax_lookup_userdata\' );
/*
* Ajax_lookup_userdata
* Will check the string on the input field for the code.
*/
function ajax_lookup_userdata() {
?>
<script type="text/javascript">
jQuery(document).ready(function( $ ) {
/*Lookup the field value.*/
var ajax_url = \'<?php echo admin_url(\'admin-ajax.php\'); ?>\';
var codeValue = jQuery(\'li.code input\');
var data;
/* Limit the keyup function... min characters and max characters. */
$(codeValue).keyup(function(e){
data = {
\'action\': \'lookup_code\',
\'codevalue\': jQuery(this).val()
};
// This gives me the output of the values you insert into the code-textfield.
console.log(data.codevalue);
jQuery.post(ajax_url, data, function(response) {
//alert(\'Got this from the server: \' + response);
console.log(data);
console.log(response);
});
});
});
</script>
add_action(\'wp_ajax_nopriv_lookup_code\', \'lookup_code\'); // for not logged in users
function lookup_code() {
global $wpdb;
$searchQuery = $_POST[\'codevalue\'];
$results = $wpdb->get_results(\'SELECT * FROM wp_contacts WHERE code LIKE "\'.$searchQuery.\'" \');
update_post_meta( $id, \'_wp_page_template\', \'new_template.php\' );
// Here are my values that I want to receive.
// How to pass them to the field values?
//var_dump($results[0]);
die();
}
var\\u转储($结果[0]);提供以下输出:
object(stdClass)#277 (8) {
["id"]=>
string(1) "1"
["code"]=>
string(2) "1a"
["firstname"]=>
string(4) "Bert"
["lastname"]=>
string(7) "Gibbers"
["street"]=>
string(15) "Lazy Berry Edge"
["no"]=>
string(2) "35"
["email"]=>
string(18) "[email protected]"
["phone"]=>
string(12) "202-555-0102"
}
非常感谢您的支持。