我有一个包含多个数据输入的表单。
现在我需要有一些东西,当用户输入他的个人号码时,其余数据将自动使用数据库中的数据完成。
个人号码的ID为input_1_2。
我有以下代码(它是一个wordpress网站/插件)
/*
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('#input_1_2 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>
*/
<?php
add_action( 'admin_footer', 'my_action_javascript' );
function my_action_javascript() {
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var data = {
'action': 'my_action',
'whatever': 1234
};
// since 2.8 ajaxurl is always defined in the admin header and points to
admin-ajax.php
$.post(ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
});
});
</script>
<?php
add_action( 'wp_ajax_my_action', 'my_action_callback' );
function my_action_callback() {
global $wpdb; // this is how you get access to the database
$whatever = intval( $_POST['whatever'] );
$whatever += 10;
echo $whatever;
die(); // this is required to return a proper result
}