1

I use this code to submit a form

<script type="text/javascript">
    var frm = $('#blogform');
    frm.submit(function (ev) {
        $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
                alert(data);
            },
            error: function (jXHR, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });

        ev.preventDefault();
    });
</script>

    <form id="blogform" action="/my_url" method="post">
        ...
    </form>

My problem is that if the submission is successful I want to be able to empty the form field.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ukor
  • 322
  • 4
  • 16

2 Answers2

2

var frm = $('#blogform'); must be var frm = $('#myform'); according to the form sample you provide in the question. And in order to reset your form, Use trigger or reset before alert(data):

$('#myform').trigger("reset");

or

$('#myform')[0].reset();

In your php code you can check whether the submission is successful or not and process that data the jquery like below:

if(data){
  $('#myform')[0].reset();
  alert(data);
}else{
 return false;
}

Hope this may help you.

Community
  • 1
  • 1
Mawia HL
  • 3,605
  • 1
  • 25
  • 46
0

You can use JavaScript reset() method.

Bill
  • 1
  • 3