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...
– DiegoSantos
This mine only takes from one, as it would to pick up on more than one form?
– Wagner Martins Bodyboard
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.
– David Dias