1
Hello, I have the following code, where the Modal Bootstrap opens from a BD search when I click on an item from the returned list:
<input type="text" name="pesquisa" id="pesquisa">
<table class="resultado">
</table>
So far so good, it returns the values correctly, opens the Modal with the correct ID, but when I click the button to run, it runs the function only with the first item of the ID column of the BD, ie, works only with ID "1", the other ID it does not return the value in content.
function reg_prod() {
$.ajax({
type: "POST",
url: "../_php/nova_entrega_reg_prod.php",
data: {
produto: $('#id').val(),
},
success: function(data) {
$('#conteudo').html(data);
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="modal fade" id="myModal<?php echo $qry['id']?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Titulo</h4>
</div>
<div class="modal-body" style="white-space: normal;">
<input type="text" name="nome" id="id" value="<?php echo $qry['id']?>">
</div>
<div id="conteudo">Teste</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
<button type="button" onClick="reg_prod()" class="btn btn-success">Registrar</button>
</div>
</div>
</div>
</div>
Here in content he should return, in this case, the same ID he opened the MODAL
<?php
$produto = $_POST['produto'];
echo $produto;
?>
Does anyone know why this happens?
What a "Run" button that would be?
– Sam
the button that executes the function -> <button type="button" onClick="reg_prod()" class="btn btn-Success">Register</button>
– ThiagoKoller
In that part here
value="<?php echo $qry['id']?>"
theid
doesn’t change...– Sam
The ajax is taking this
id
that never changes...– Sam
is that if you go to see, this stretch is inside the modal, which will open according to the ID, then, every id I open with the modal, it appears in the input, the id related to the modal. only when executing the function, it only works when I open the modal for ID 1..
– ThiagoKoller
Yes. But this id is coming from PHP, and you are only changing the content div... the input id
name="nome"
will always be the same.– Sam
when I open the modal of ID 2, it appears there in the input, ID 2, however it seems that the ajax does not execute, how could I make the ajax take the value of the input ID referring to the open modal? it is possible?
– ThiagoKoller
Let’s go continue this discussion in chat.
– Sam