Clear fields with Ajax

Asked

Viewed 1,522 times

0

Colleagues.

I took a project that already includes the shooting of emails with PHP/Ajax, the shooting occurs normally, but the fields are not cleaned after the shooting. I tried to put form.value(); but it still doesn’t work. Below is the code:

/* Contact form ajax Handler
    ================================================*/

    $(".ajax-form").on('submit', function() {
        var form = $(this);
        var formURL = $(this).attr("action");
        var postData = $(this).serializeArray();

        $.ajax({
            url: formURL,
            type: 'POST',
            data: postData,
            dataType: 'json',

            success:function(data, textStatus, jqXHR){

                if(data.success==1){

                    form.find(".alert").fadeOut();
                    form.find(".alert-success").html(data.message);
                    form.find(".alert-success").fadeIn(600);


                }else{

                    form.find(".alert").fadeOut();
                    form.find(".alert-danger").html(data.message);
                    form.find(".alert-danger").fadeIn(600);

                }
            },

            error: function(jqXHR, textStatus, errorThrown)  { 

                console.log(errorThrown);
            }

        });


        return false;
     })

1 answer

1


Do so, the command to clear the form in case would be this

$('.ajax-form')[0].reset();

just you put in the part you want to execute this function in the case I believe it is within the scope of the Success

this command would serve

$(".ajax-form").trigger('reset');
  • Perfect Jasar. Thank you very much. I will give as acceptable your reply, however I have to wait 3 minutes.

  • out of nowhere I helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.