WordPress plugin admin page – using WordPress function in linked php file

PLEASE!! PLEASE!! Don’t send the php function names from your javascript. Below is the correct way of doing it. It might not be best way but at least secure way.

To register your javascript (see the use of plugins_url instead of path_join), Also the validation is now part of your plugin, it does not have to go in separate validation.php:

function on_screen_validation() {
    wp_enqueue_script( "field_validation", plugins_url( "field-validation.js", __FILE__ ), array( 'jquery' ) );
    wp_localize_script( "field_validation", "MySecureAjax", array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'admin_print_scripts', 'on_screen_validation' );

add_action( 'wp_ajax_UserNameValidation', 'validateUserName' );

function validateUserName() {
    $username = $_POST['thevars'];
    if ( username_exists($username) ) {
        echo $username.' does exist'; 
    } else {
        echo $username.' doesnt exist';
    }

    // Don't remove exit
    exit;
}

You javascript part:

jQuery(document).ready(function() {
    //run field validation on username on blur
    jQuery('.valusername').blur(function() {

        var usernameVal = jQuery(this).val();
        var action = 'UserNameValidation';

        jQuery.post( MySecureAjax.ajaxurl, {action: action, thevars: usernameVal}, function(data) {
             alert(data); //would update validation message here
        });
    });
});

You can add nonce checking for further security and validation.