I have several forms in my code that I need to send the information with ajax to PHP, it is returning this error:
Uncaught ReferenceError: formID is not defined
It says that the formID was not defined, but in my forms they are appearing, how would I go about solving this and submitting information for each form?
Jquery
$(document).ready(function() {
$('#selecionar_empresa_form_'+formID).submit(function() {
//Remove a palavra quitar_ e deixa somente "debitoX"
//Captura o elemento que sofreu o evento de "submit"
const formDetails = $(this);
$.ajax({
type: "POST",
url: 'selecionar_empresa.php',
data: formDetails.serialize(),
success: function (data) {
// Inserting html into the result div
$('.selecionar_empresa_'+formID).html(data);
},
error: function(jqXHR, text, error){
// Displaying if there are any errors
$('.selecionar_empresa_'+formID).html(error);
}
});
return false;
});
});
Form
<form id="selecionar_empresa_form_<?php echo $i ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="i" value="<?php echo $i ?>" class="i">
<input type="hidden" name="id_usuario_selecinadas" value="<?php echo $id_usuario ?>" class="id_usuario_selecinadas">
<input type="hidden" name="selecionar_empresa" id="selecionar_empresa_<? echo $curnm['id'] ?>" value="<? echo $curnm['id'] ?>" />
<div class="resultado_empresa_selecionada_<?php echo $i ?>">
<button type="submit"><img src="images/add-star.png" class="img-responsive" style="float:right;max-width: 24px;" /> </button>
</div>
</form>
I did this and returned this error
formDetails is not defined
<form id="selecionar_empresa_form_<?php echo $i ?>" class="formAjax" data-formid="<?=$i?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="i" value="<?php echo $i ?>" class="i">
<input type="hidden" name="id_usuario_selecinadas" value="<?php echo $id_usuario ?>" class="id_usuario_selecinadas">
<input type="hidden" name="selecionar_empresa" id="selecionar_empresa_<? echo $curnm['id'] ?>" value="<? echo $curnm['id'] ?>" />
<div class="resultado_empresa_selecionada_<?php echo $i ?>">
<button type="submit"><img src="images/add-star.png" class="img-responsive" style="float:right;max-width: 24px;" /> </button>
</div>
</form>
JS
//Executa em cada form:
$('.formAjax').on("submit",function() {
// Pegar o ID do formulário para depois:
var formID=formDetails.data("formid");
//Remove a palavra quitar_ e deixa somente "debitoX"
//Captura o elemento que sofreu o evento de "submit"
const formDetails = $(this);
$.ajax({
type: "POST",
url: 'selecionar_empresa.php',
data: formDetails.serialize(),
success: function (data) {
// Inserting html into the result div
$('.selecionar_empresa_'+formID).html(data);
},
error: function(jqXHR, text, error){
// Displaying if there are any errors
$('.selecionar_empresa_'+formID).html(error);
}
});
return false;
});