I have a form for a user to fill out. On the server-side, I'm using mailgun to send an email once the form has been submitted.
Upon an error, I want to display a toast dialog (Materialize.toast("Error", 2500);
in Javascript). Since the error checking happens on the server side, I'm not sure how to call that method.
I happen to be using a <script>
inside the HTML to stop the page from refreshing upon a submission though--but I couldn't figure out how to handle errors in this <script>
. Here it is:
<script type="text/javascript">
$("#contact-form").submit(function(e) {
e.preventDefault(); // Prevents the page from refreshing
var $this = $(this); // `this` refers to the current form element
$.post(
$this.attr("action"), // Gets the URL to sent the post to
$this.serialize(), // Serializes form data in standard format
function(data) {
},
"json" // The format the response should be in
);
Materialize.toast("Toasty McToastface!", 1000);
$('#contact-form').trigger('reset');
});
</script>
I appreciate any and all help.