I am not very familiar with jQuery. I am trying to create a form that will be sent in the background without reloading the page.
I have a hidden div that shows and hides the click, inside the div there is a form.
I have two problems:
1) If the form verification fails, the form is still submitted. I tried to put the verification and sending codes in the state if(validation == valid) { $.ajax.... } , but it does not work properly.
2) After submitting the form, the div is automatically hidden, so a successful message is not displayed.
Here is the code:
$().ready(function() { // Validate the form when it is submitted, using validation plugin. var validator = $("#contactform").validate({ errorContainer: container, errorLabelContainer: $(), onkeyup: false, onclick: false, onfocusout: false, errorPlacement: function (error, element) { error.insertBefore(element); } }); }); $(function() { //This submits a form $('input[type=submit]').click(function() { $.ajax({ type: "POST", url: "contact.php", data: $("#contactform").serialize(), beforeSend: function() { $('#result').html('<img src="loading.gif" />'); }, success: function(data) { $('#result').html(data); } }) }) }) //This shows and hides div onclick $(document).ready(function(){ $(".slidingDiv").hide(); $(".show_hide").show(); $('.show_hide').click(function(){ $(".slidingDiv").slideToggle(); }); });
source share