Jonathan Jonathan - 3 months ago 13x
jQuery Question

how to clear form after successful submit ajax

I would like to make the contact form disappear after successful submission but am not sure how to go about it. Any assistance would be appreciated.

jQuery(document).ready(function($) {
$("#ajax-contact-form").submit(function() {

var str = $(this).serialize();

type: "POST",
url: "contactfinal.php",
data: str,
beforeSend: function() {
success: function(response) {

return false;


Assuming you perform server side validation, you should check if the response is empty, then hide the form:

success: function(response) {
    if (!response || response.length == 0)

That's the ideal way, however based on seeing your source code, you need a "hack":

if (!response || response.length == 0 || response.indexOf("Your message was sent") >= 0)