Skip to content

Instantly share code, notes, and snippets.

@AaronSadlerUK
Last active May 5, 2022 11:30
Show Gist options
  • Save AaronSadlerUK/f6e9b3b25166bbcd96b3e846453fb8cb to your computer and use it in GitHub Desktop.
Save AaronSadlerUK/f6e9b3b25166bbcd96b3e846453fb8cb to your computer and use it in GitHub Desktop.
Umbraco Forms (Single Page) submit within bootstrap modal using ajax
// jQuery plugin to prevent double submission of forms
jQuery.fn.preventDoubleSubmission = function () {
$(this).on('submit', function (e) {
e.preventDefault();
var modal = $(this);
var $form = $(this).children('form').first();
if ($form.data('submitted') === true) {
// Previously submitted - don't submit again
} else {
if ($form.valid()) {
// Mark it so that the next submit can be ignored
$form.data('submitted', true);
// Make ajax call form submission
$.ajax({
url: $form.attr('action'),
type: 'POST',
cache: false,
data: $form.serialize(),
success: function (result) {
// console.log(result);
var thankYouMessage = $(result).find('.umbraco-forms-submitmessage');
modal.html(thankYouMessage);
}
});
}
}
});
// Keep chainability
return this;
};
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.16.0/jquery.validate.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/mvc/5.2.3/jquery.validate.unobtrusive.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.unobtrusive-ajax/3.2.5/jquery.unobtrusive-ajax.js"></script>
<script>
$(document)
.ready(function() {
//Intercept Submit button in order to make ajax call instead of a postback
$('#umbraco_form_@(Guid.Parse((string)Model.YOURFORMID).ToString("N"))').preventDoubleSubmission();
});
</script>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal Title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body px-3">
@{ Html.RenderAction("RenderForm", "UmbracoForms", new { formId = Model.YOURFORMID, theme = "YOURTHEME", includeScripts = false }); }
</div>
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment