There is a form. Just as I did not try to use the reset-no sense. Any error, or the form is not cleared. How can I clear all the fields inside the form?
Here is the code:

jQuery('#contact-form').submit(function(e) { e.preventDefault(); var c_name = jQuery('#c_name').val(); var c_email = jQuery('#c_email').val(); var c_message = jQuery('#c_message ').val(); var response = jQuery('#contact-form .ajax-response'); var formData = { 'name': c_name, 'email': c_email, 'message': c_message }; if ((c_name == '' || c_email == '' || c_message == '') || (!isValidEmailAddress(c_email))) { response.fadeIn(500); response.html('<i class="fa fa-warning"></i> Исправьте ошибки в форме и попробуйте снова.'); } else { jQuery.ajax({ type: 'POST', // define the type of HTTP verb we want to use (POST for our form) url: 'php/contact.php', // the url where we want to POST data: formData, // our data object dataType: 'json', // what type of data do we expect back from the server encode: true, success: function(res) { var ret = jQuery.parseJSON(JSON.stringify(res)); response.html(ret.message).fadeIn(500); } }); response.html('<i class="fa fa-warning"></i> Ваше сообщение отправлено, Спасибо за Ваш Выбор!'); } return false; }); 

I tried after sending to contact # contact-form, but nothing comes out.

  • In the succes add cleaning. - Mstislav Pavlov
  • success: function (res) {... alert ('check'); } I even tried to do so — the alert does not work - HoPkInS
  • It means your sending is not successful. - Mstislav Pavlov
  • @ MstislavPavlov, how can it not be successful if the letter arrives in the mail ?! - HoPkInS
  • For example, you return an incorrect http code. - Mstislav Pavlov

2 answers 2

  success: function(res) { ... jQuery('#contact-form')[0].reset(); } 
  • So I thought so too. But it doesn’t work — there are no errors. And today I was visited by a clever idea and I just inserted an alert to check, and it doesn’t execute inside success ... there are no errors in the console. What could be the problem? - HoPkInS
  • Add an error: function(jqXHR, textStatus, errorThrown) { alert(textStatus + " - " + errorThrown + ": " + jqXHR.responseText) } handler error: function(jqXHR, textStatus, errorThrown) { alert(textStatus + " - " + errorThrown + ": " + jqXHR.responseText) } to the jQuery.ajax parameter. - Igor

For example: you can do it in two ways, the first is to add a class to each field (for example, .reset ) and clear all fields by class

 $('.reset').val(''); 

or clear each field separately.

 $('#c_name').val(''); $('#c_email').val(''); $('#c_message').val(''); 

This code needs to be inserted in the middle of the res function.

 success: function(res) { $('.reset').val(''); var ret = jQuery.parseJSON(JSON.stringify(res)); response.html(ret.message).fadeIn(500); } 
  • For some reason, even the alert inside res does not work for me. So it shouldn't be. Right? - HoPkInS