Created
March 11, 2020 11:48
-
-
Save wpmudev-sls/beb8f01006f97bf05c2238b4be3ab198 to your computer and use it in GitHub Desktop.
[Forminator Pro] - Make an AJAX request upon an input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Forminator Pro] - Make an AJAX request upon an input | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Send an AJAX request when an input is filled (as of 1.11.3) | |
* Author: Alessandro Kaounas @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* Task: 0/1135022585412927/1164144055767340 | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
// No need to do anything if the request is via WP-CLI. | |
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
return; | |
} | |
add_action( 'wp_footer', 'wpmudev_form_ico_ajax_request', 99 ); | |
function wpmudev_form_ico_ajax_request(){ ?> | |
<script type="text/javascript"> | |
(function ($, document, window) { | |
"use strict"; | |
(function () { | |
let form_id = 403, field_id = 'text-7'; | |
$(document).on( 'after.load.forminator',function( e, form_id ) { | |
let form = $( '#forminator-module-' + form_id ), | |
element = form.find( '#' + field_id + ' .forminator-input' ), | |
limit = element.closest( '.forminator-row' ).find( '[data-limit]' ).data( 'limit' ), | |
ajax = false; | |
element.on( 'keyup', function(){ | |
let input = $(this); | |
if( input.val().length === limit ){ | |
if( ajax ) return; | |
$.ajax({ | |
url: "ares.php", | |
contentType: "application/json; charset=utf-8", | |
dataType: "json", | |
data: "ico=" + input.val(), | |
cache: false, | |
beforeSend: function(){ | |
ajax = true; | |
input.blur(); | |
form.addClass('forminator-fields-disabled'); | |
}, | |
complete: function(){ | |
ajax = false; | |
form.removeClass('forminator-fields-disabled'); | |
}, | |
success: function ( data ) { | |
// Do some things here... | |
}, | |
error: function ( data, errorThrown ) { | |
console.log( 'Error response: ' + errorThrown ); | |
} | |
}); | |
} | |
}); | |
}); | |
})(); | |
}(jQuery, document, window)); | |
</script> | |
<?php } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment