-1

I have a Ask Question form by which client can ask any question to us. While client submit the form we will give him a thanks message for 5 seconds then hide the message and show again the Ask Question form in that div.

But while the from comes after successful message, form input values is still there. I want a fresh from without any input value like the form before submitting.

Fill the form for asking a question

Give thanks message after successfully submitted

Auto new form comes after shown successful message for 5 seconds. But still the previous input values there

Here is my ajax code

$.ajax({

            url:"{{ url('/ask_question_form') }}",
            type: 'GET',
            data: {_token :token, name : name, email : email, contact : contact, question : question},
            success:function(msg){

                // console.log(msg);

                $('.question-modal .textwidget').hide();

                trHTML = "";
                trHTML += "<div id='user-question' style='margin-top:50%; color:#0071BC'>";

                trHTML += "Thanks for your question. We will save your question for further query and give a feedback as soon as possible.";

                trHTML += "</div>";

                $('.question-modal').append(trHTML);

                setTimeout(function() {
                    $('.question-modal #user-question').remove();
                    $('.question-modal .textwidget').show();
                }, 5000);


            }
        });

Where textwidget is the class name of that form

Cœur
  • 37,241
  • 25
  • 195
  • 267
Arafat Rahman
  • 574
  • 3
  • 11
  • 27

5 Answers5

1

The best solution is-

$("#form")[0].reset();

use this code when your success message comes.

Priyal Pithadiya
  • 889
  • 1
  • 5
  • 12
1

After completing 5 seconds and when you hide the "success message" at that time you can reset your form

$("#formId").get(0).reset();

Here "formId " id of your form.

Chintan Kukadiya
  • 784
  • 5
  • 16
0

Erase values of all fields once you successfully submit the form.

document.querySelector('get-your-field1').value = '';
document.querySelector('get-your-field2').value = '';
document.querySelector('get-your-field3').value = '';
document.querySelector('get-your-textarea').value = '';

Or you can use form reset.

const form = document.querySelector('form');
const btn = document.querySelector('button');

btn.addEventListener('click', event => {
  event.preventDefault();
  form.reset();
});
<form>
  <input />
  <input />
  <button type="submit">submit</button>
</form>
Matus Dubrava
  • 13,637
  • 2
  • 38
  • 54
0

Here is an example to do a form reset function:

function reset(){
    setTimeout(function(){    
      var form = document.getElementById('myForm');
      form.reset();
    }, 1000);
}
var btn = document.getElementById('btn');
btn.onclick = function(){
  reset();
}
<form id="myForm">

  <input value="" />
  <br />
  <input value="" />
  <br />
  <input value="" />
  <br />
  <input value="" />
  <br />

  <button id="btn" type="button">reset</button>
</form>
Terry Wei
  • 1,521
  • 8
  • 16
-1

Please note, that it seems that you are using some token along with the form.

This means, that if you reset the values, this token will still be there, and maybe the server will not accept the next form submission, due the duplicate use of that token.

You will need to set a new token, when the server respond.

JohnnyJS
  • 1,320
  • 10
  • 21