Execute ajax on page with more than one form

Asked

Viewed 111 times

0

I have this Ajax code that makes the request for a PHP file, when there is only one form on the page, it works right.

How do I make it work when you have more than one form?

JS that works

$(document).on("submit", "#quitar_debito", function(event)
{
    $(".resultado_debito").html('<div class="spinner"></div>');
            event.preventDefault(); 
var form = $('#quitar_debito');
            $.ajax({
                url: 'enviar_pagamento.php',
                type: 'POST',
                data: form.serialize() 
            })
            .done(function(data){
                $('.resultado_debito').fadeOut('slow', function(){
                    $('.resultado_debito').fadeIn('slow').html(data);
                });
            })
            .fail(function(){
                alert('Ajax Submit Failed ...');    
            });
}); 

HTML that works

<form method="post" id="quitar_debito" novalidate="novalidate">

   <input type="hidden" name="pagamento" value="" size="sim" class="pagamento">

   <input type="hidden" name="id_empresa_pagamento" value="<?php echo $row["id"]?>" class="id_empresa_pagamento">

   <input type="submit" value="Cadastrar">

</form>

Form html on the same page that doesn’t work

    <div class="resultado_debito_0">
  <form method="post" id="quitar_debito_0" novalidate="novalidate">

   <input type="hidden" name="pagamento" value="" size="sim" class="pagamento">

   <input type="hidden" name="id_empresa_pagamento" value="<?php echo $row["id"]?>" class="id_empresa_pagamento">

   <input type="submit" value="Cadastrar">

</form>
</div>


    <div class="resultado_debito_1">
  <form method="post" id="quitar_debito_1" novalidate="novalidate">

   <input type="hidden" name="pagamento" value="" size="sim" class="pagamento">

   <input type="hidden" name="id_empresa_pagamento" value="<?php echo $row["id"]?>" class="id_empresa_pagamento">

   <input type="submit" value="Cadastrar">

</form>
</div>


    <div class="resultado_debito_2">
  <form method="post" id="quitar_debito_2" novalidate="novalidate">

   <input type="hidden" name="pagamento" value="" size="sim" class="pagamento">

   <input type="hidden" name="id_empresa_pagamento" value="<?php echo $row["id"]?>" class="id_empresa_pagamento">

   <input type="submit" value="Cadastrar">

</form>
</div>

js with problem

$(document).on("submit", "form", function(event)
{
    $(".resultado_debito").html('<div class="spinner"></div>');
            event.preventDefault(); 

var form_data = new FormData(); 
var id = $(this).attr('id');
            $.ajax({
                url: 'enviar_pagamento.php',
                type: 'POST',
                data: form_data
            })
            .done(function(data){
                $('.resultado_debito').fadeOut('slow', function(){
                    $('.resultado_debito').fadeIn('slow').html(data);
                });
            })
            .fail(function(){
                alert('Ajax Submit Failed ...');    
            });
});
  • What happens after Submit? Error? What does . js dick and what do you want to do? Your js just get Submit from a form...

  • This mine only takes from one, as it would to pick up on more than one form?

  • The problem there is that you are picking it up by the ID, and so only one. You need to pick it up by the class, for example. Or just take the element form.

1 answer

0


The problem is in the form selector.

$(document).on("submit", "#quitar_debito", function(event)

Try:

This will take all the forms in the document:

$(document).on("submit", "form", function(event)

Take the id of the form that triggered the event:

var id = $(this).attr('id')

Inside your script, in the form call, you can do so direct:

var form = $(this);

Explanatory Edit: The ID attribute literally makes the element have a unique identification in HTML. So, the id selector does not return an array of elements, just one.

To solve your case, you could use per class, because yes class returns an array in the selector.

My suggestion was for you to actually take the form element, regardless of its identification.

After that, once you are inside the executed event, you can get the element through the variable $(this). That is, the form you were looking for and defining the var form =$('#quitar_debito');, is exactly the $(this). Can do var form = $(this) that will still.

Realize that it is only one of the possible solutions.

  • I did this, but only so it shows results in all forms, because the id to show the result has to change form to form

  • Then you’ll need to take the $(this).attr('id') form within the call and use this id.

  • how I would do it in my code?

  • See if the issue helps you

  • Wagner, I’ve simplified the answer a little bit. I think now Voce will understand

  • and to show the result, would have to be div with different id, as would?

Show 2 more comments

Browser other questions tagged

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