1

How do I clear a form data once the submit succeed in jquery?

   $(document).ready(function () {
       $('#submit').click(function () {
           $.ajax({
              type: "POST",
              url: 'final.php',
              data: "user=" + $('#user').val() + "&comment=" + $('#comment').val(),
              success: function (data) {

                  $('#status').html(data);
               }
          });
       });
   });

HTML... I need to clear the form data once the form is submitted....Any help would be appreciated.

<input type="text" name="user" id="user">
<input type="text" name="comment" id="comment">
<input name="submit"  type="submit" id="submit">
<div id="status">
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
user2510039
  • 167
  • 3
  • 5
  • 13

2 Answers2

7
 $('#formId').trigger('reset');
Nick B
  • 657
  • 1
  • 13
  • 33
5

$('form').find('input[type=text]').val('') should do

Call this in the success callback of your Ajax

Sushanth --
  • 55,259
  • 9
  • 66
  • 105
  • I did but it is not working. I put the code after $('#status').html(data); – user2510039 Jun 21 '13 at 18:46
  • 1
    Do you have a form in the first place.. Otherwise just use `$('input[type=text]').val('')` – Sushanth -- Jun 21 '13 at 18:47
  • tried that again, doesn't work. if you mean the
    I am not using that, it is sending the id to the final.php...
    – user2510039 Jun 21 '13 at 18:52
  • Because you have not mentioned anything regarding the form... Try this `$(this).closest('form').find('input[type=text]').val('')` .. I doubt the success callback is being fired in the first place. Check you console for errros – Sushanth -- Jun 21 '13 at 18:55
  • Also `I am not using that, it is sending the id to the final.php.` ?? .. Does not make senses.. Can you explain – Sushanth -- Jun 21 '13 at 18:55
  • I mean like I have final.php page and I don't have
    on my html page. All I have on the text fields is the name and id attribute. What I mean by sending was, I don't know but the jquery at the top sends the name attribute to final.php and it is working perfectly except that it doesn't clear the data from the text fields.
    – user2510039 Jun 21 '13 at 19:02
  • Then `$('input[type=text]').val('')` should work for you unless you have any errors on the page – Sushanth -- Jun 21 '13 at 19:04