0
I have several formats in my code that I need to send the information with ajax to PHP, is returning this error:
Uncaught Referenceerror: formID is not defined
It says that the formID has not been set, but on my forms they are appearing, as I would do to resolve this and send the information of 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 so and returns this error
formDetails is not definied
<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;
});
I did as Oce said, returns this error in the formDetalis console is not definied, look like I did in my question, I edited it
– Wagner Martins Bodyboard
Ah, my mistake. I pulled the formDetails before setting it, I will edit my answer.
– Henrique Pauli