Last active
March 22, 2017 09:51
-
-
Save eonarik/c13d3b0bf02503ea48f6fa5eb87a532b to your computer and use it in GitHub Desktop.
modx js form
This file contains 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
alertify.set('notifier','position', 'top-right'); | |
let CONNECTOR_URL = '/assets/components/modxsite/connectors/connector.php'; | |
var isRequest = false; | |
$(document).on('click','[data-action] [type=submit]',function(e){ | |
e.preventDefault(); | |
if(!isRequest){ | |
isRequest = true; | |
var mask = $('<div></div>').css({ | |
position: 'absolute', | |
top: 0, | |
bottom: 0, | |
left: 0, | |
right: 0, | |
background: 'rgba(0,0,0,.5)', | |
'z-index': 1, | |
}); | |
let form = $(this).parents('form:first').css({ | |
position: 'relative', | |
}); | |
var formData = new FormData(form[0]); | |
var error = $('<div class="js-form-notice"></div>').css({ | |
'color': 'red', | |
'font-size': '80%', | |
}); | |
$.ajax({ | |
url: CONNECTOR_URL +'?pub_action='+ form.attr('data-action'), | |
type: 'POST', | |
data: formData, | |
contentType: false, | |
processData: false, | |
error: function(e){ | |
console.log(e); | |
}, | |
success: function(r){ | |
// console.log(r); | |
if(r.success){ | |
form[0].reset(); | |
alertify.success(r.message); | |
// form.find('.js-success-message').html('<h2 class="js-form-notice">'+ r.message +'</h2>') | |
} else { | |
for(var i in r.data){ | |
var field = form.find('[name='+ r.data[i].id +']'); | |
alertify.error(r.data[i].msg); | |
field.parents('.form-control:first').addClass('has-error'); | |
field.after(error.clone().text(r.data[i].msg)); | |
} | |
alertify.error(r.message); | |
// form.prepend(error.clone().css({ 'font-size': '120%' }).text(r.message)) | |
} | |
}, | |
complete: function(){ | |
isRequest = false; | |
mask.remove(); | |
form.find('.has-error').removeClass('has-error'); | |
form.find('.js-form-notice').remove(); | |
} | |
}); | |
} | |
return false; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment