1

I have a form with several data inputs.

Now I need to have something that when the user inputs his personal number, the rest of the data is being completed automatically with data from the database.

The id of the personal number is input_1_2.

I have the following code (it's a wordpress website/plugin)

    /*
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
}
stijn.aerts
  • 6,026
  • 5
  • 30
  • 46

0 Answers0