Last active
December 7, 2015 14:36
-
-
Save airways/2cd9e09f07d31431f1e3 to your computer and use it in GitHub Desktop.
ProForm Ajax Example Template -- see http://www.manula.com/manuals/rawaysmith/proform/1.65/en/topic/sample-full-form-template
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
<script type="text/javascript" > | |
// So the JS knows where our files are | |
var site_url = "{site_url}"; | |
</script> | |
{!-- The AJAX code should work with the default HTML generated by the simple form tag: --} | |
{exp:proform:simple form_name="contact_us"} | |
<script type="text/javascript" src="http://localhost/index.php//assets/js/contact_us.js"></script> |
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
$(document).ready(function() { | |
ContactUs.init(); | |
}); | |
var ContactUs = { | |
$form: undefined, | |
$success: undefined, | |
init: function() { | |
var self = this; | |
self.$form = $('#contact_us_proform'); | |
self.$success = $('.indicator.success'); | |
self.$success.hide(); | |
self.bind(); | |
}, | |
bind: function() { | |
var self = this; | |
self.$form.submit(function(e){ | |
// Stop default submission | |
e.preventDefault(); | |
// Remove any old messages | |
self.$form.find('.indicator.error').remove(); | |
self.$form.find('.indicator.success').hide(); | |
self.$form.find('li').removeClass('error'); | |
// Disable the form to prevent double clicks. | |
self.$form.find('input[type=submit]').attr('disabled', true); | |
// Show an indicator so the user knows something is happening | |
self.$form.find('.submit_btn').after('<img src="'+ site_url + 'themes/cp_themes/default/images/indicator.gif" class="spinner" />'); | |
$.ajax({ | |
type: "POST", | |
url: self.$form.attr('action'), | |
data: self.$form.serialize(), | |
dataType: "json" | |
}).done(function(msg) { | |
// Remove it... | |
self.$form.find('.spinner').remove(); | |
if(msg.status == "error") | |
{ | |
// Re-enable the form if there are errors | |
self.$form.find('input[type=submit]').attr('disabled', false); | |
for(error in msg.errors) | |
{ | |
switch(error) | |
{ | |
case 'email_address' : | |
$('input[email_address]').after('<span class="indicator error">Please enter a valid email</span>').closest('li').addClass('error'); | |
break; | |
case 'name' : | |
$('input[name=name]').after('<span class="indicator error">Please enter your name</span>').closest('li').addClass('error'); | |
break; | |
case 'captcha' : | |
$('input[name=captcha]').after('<span class="indicator error">Please enter the correct captcha</span>').closest('li').addClass('error'); | |
break; | |
} | |
} | |
if($('.form_captcha')) { | |
$('.form_captcha').html(msg.new_captcha); | |
} | |
} | |
else if(msg.status == "success") | |
{ | |
self.$success.show(); | |
self.$form.find('li').removeClass('error'); | |
} | |
}); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment