In the WordPress theme my client is using the contact form is submitted via Ajax.
Form Markup
<form action="#" method="post" id="contactform">
<input type="text" class="required" name="name" id="name" />
<button name="send" type="submit" id="submit">Send</button>
<div id="success"></div>
<div id="failure"></div>
</form>
contact_form.js
(function ($) {
function themeContactForm() {
function start() {
//binds the form elements here
}
function submit(e) {
//check for form validation
return false;
}
function send(data) {
jQuery.post(
ajaxurl,
{
action : 'themeContactForm',
data : data
},
result
);
}
function result(response) {
if(response.success) {
//show success message
} else {
//show failure message
}
}
start();
}
$.fn.themeContactForm = function() {
};
}(jQuery));
app.js
$("#contactform").themeContactForm();
Issue : After clicking submit, response of success or failure takes around 3-4 secs to appear. During that time users probably think nothing is happening and they click submit again. As a result multiple copies of the same form data is received.
Question : Without touching any of the above mentioned files, is it possible to show a "spinner" when submit is clicked and hide it when the success or failure messages comes from the script, so the user know that the form is being processed.
I can modify contact_form.js and get my job done, but on theme update modifications will be lost.
I have tried binding to ajaxComplete event, but somehow can't get it to work.
$( document ).ajaxComplete(function( event, xhr, settings ) {
alert("Global event called");
console.log( xhr.responseText );
});
But the alert isn't firing at all.