Two Ajax Form on a single page with multiple items

Asked

Viewed 34 times

0

I can make an Ajax request as follows:

HTML

<div class="resultado_debito<?php echo $i ?>">
  <span class="ruim"><br>Inadiplente</span><br>

  <form method="post" id="quitar_debito<?php echo $i ?>" novalidate="novalidate">

   <input type="hidden" name="pagamento" value="sim"  class="pagamento">
   <input type="hidden" name="i" value="<?php echo $i ?>"  class="i">   
   <input type="hidden" name="id_empresa_pagamento" value="<?php echo $row["id"]?>" class="id_empresa_pagamento">
   <input type="submit" value="Quitar">     
</form>
</div>

Js Ajax

$(document).ready(function() {
    $("form").submit(function() {

        //Remove a palavra quitar_ e deixa somente "debitoX"
        const formID = $(this).attr('id').replace("quitar_", "");

        //Captura o elemento que sofreu o evento de "submit"
        const formDetails = $(this);

        $.ajax({
            type: "POST",
            url: 'enviar_pagamento.php',
            data: formDetails.serialize(),
            success: function (data) { 
                // Inserting html into the result div
                $('.resultado_'+formID).html(data);
            },
            error: function(jqXHR, text, error){
                // Displaying if there are any errors
                $('.resultado_'+formID).html(error);           
            }
        });
        return false;
    });
});

My question would be how I can put one more form on the same page with Ajax request for another file.

Note that the Form ID varies according to the database record ($i), because ajax runs on all bank records.

  • 1

    Well, you’re already receiving the requests that any form fires... With respect to the target, each form can have its action itself, via HTML. This way, forget the id. Return to this function, the form you gave to Submit. This way, you can edit the correct tag. Can you understand or need help?

  • I did not understand very well, would just put an action in each form? Could you give me an example?

  • I will reply with a suggestion

1 answer

0

Example Suggestion:

$(document).ready(function() {
    $("form").submit(function(event ) {
        event.preventDefault();
        
        //Captura o elemento que sofreu o evento de "submit"
        const formDetails = $(this);
        var dataIdx = formDetails.getAttribute('data-idx');
        
        $.ajax({
            type: formDetails.getAttribute('method'),
            url: formDetails.getAttribute('action'),
            data: formDetails.serialize(),
            success: function (data) { 
                // Inserting html into the result div
                $('.resultado_debito'+dataIdx).html(data);
            },
            error: function(jqXHR, text, error){
                // Displaying if there are any errors
                $('.resultado_debito'+dataIdx).html(error);
            }
        });
        return false;
    });
});
<div class="resultado_debito<?php echo $i ?>">
  <span class="ruim"><br>Inadiplente</span><br>

  <form method="post" novalidate="novalidate" action="enviar_pagamento.php" method="post" data-idx="<?php echo $i ?>">

   <input type="hidden" name="pagamento" value="sim"  class="pagamento">
   <input type="hidden" name="i" value="<?php echo $i ?>"  class="i">   
   <input type="hidden" name="id_empresa_pagamento" value="<?php echo $row["id"]?>" class="id_empresa_pagamento">
   <input type="submit" value="Quitar">     
   <div class="resultado">
   </div>
</form>
</div>

  • In this example, the '.result is inside the form. If you are outside, and need to pick up hair ID, I suggest you pass the id on the form with a type attribute data-resultId="1" , and get the result with getattribute I used as an example...

  • so it gives the refresh on the page the result would have to stay inside the resultado_debito<? id div ?>

  • Look now. what happens... (I edited above)

  • is now returning an error in the Uncaught Typeerror console: formDetails.getattribute is not a Function var dataIdx = formDetails.getattribute('data-idx');

  • Question: What is the content of formDetails? And, try instead of getattribute, in this case, switch to .date('idx'). Replace the other getattribute with . attr('). Put inside the quotation marks the attribute in question

Browser other questions tagged

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