Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

to submit a form among multiple forms using jquery-ajax

i have multiple forms with same class name

<form >
  <input type="hidden" value="<%=ids%>" name="idd">
  <input type="hidden" value="<%=email%>" name="cby">
  <input type="text" class="cmd" name="cm" style="width:300px;" placeholder="comment">
  <input type="submit" value="" style="display:none;">
</form>
  <!-- n number of forms are  generated using while loop-->
<form>
  <input type="hidden" value="<%=ids%>" name="idd">
  <input type="hidden" value="<%=email%>" name="cby">
  <input type="text" class="cmd" name="cm" style="width:300px;" placeholder="comment">
  <input type="submit" value="" style="display:none;">
</form>

Then how can i submit a single form among this n number of forms i tried using

   $(function () {
          $('form').on('submit', function (e) {
                $.ajax({
                 type: 'post',
                  url: 'addfr.jsp',
                  data: $('form').serialize(),
                  success: function () {
                  location.reload();

                  }
          });
         e.preventDefault();
     });
  });

But it is submitting always 1st form among the n-forms. How can submit a random form among the n-forms. May any one help me please.

like image 543
Harika Choudary Kanikanti Avatar asked Oct 04 '13 13:10

Harika Choudary Kanikanti


2 Answers

You need to reference the form with this when you serialize it...

$(function () {
    $('form').on('submit', function (e) {
        $.ajax({
            type: 'post',
            url: 'addfr.jsp',
            data: $(this).serialize(),
            success: function () {
                location.reload();
            }
        });
        e.preventDefault();
    });
});
like image 139
Reinstate Monica Cellio Avatar answered Nov 02 '22 13:11

Reinstate Monica Cellio


Use this keyword. It will submit the form on which the event has been triggered.

why are u using location.reload?

$('form').on('submit', function (e) {
    $.ajax({
    type: 'post',
    url: 'addfr.jsp',
    data: $(this).serialize(),
    success: function () {
        location.reload();
    }
 });
like image 35
codingrose Avatar answered Nov 02 '22 14:11

codingrose