1
I have a page with a table that lists the necessary information and in it several buttons like delete, change, view. The problem is that when I click on visualize (calls the MODAL) it should list a detailed information regarding that selected record, but I have a problem regarding the id passage to make the query and present it on the screen.
Follows code:
<?php include ("cabecalho.php");?>
<?php include ("conexao.php");?>
<?php include ("funcoesBD.php");?>
<?php
//verificaUsuario();
if(isset($_POST['submit'])){
$nome = $_POST['nome'];
$produtos = buscaProdutoNome($conexao, $nome);
}else{
$produtos = listaProdutos($conexao);
}
?>
<form action="consulta-produto.php" method="post">
<td>
<label for="nome">Nome:</label>
<input type="text" name="nome">
</td>
<button type= "submit" name="submit" style="border-radius: 3px; width: 80px; height: 30px;"><b>Pesquisar</b></button>
</form>
<div class="table-wrapper">
<table class="table table-hover">
<thead >
<tr>
<th style="display: fixed;">Nome</th>
<th>Preco</th>
<th>Cor</th>
</tr>
</thead>
<?php
foreach($produtos as $produto) :
?>
<tbody>
<tr>
<!--<td><?= $produto['id'] ?></td>-->
<td><?= $produto['nome'] ?></td>
<td><?= $produto['preco'] ?></td>
<td><?= $produto['cor'] ?></td>
<td>
<a class="btn btn-primary" href="form-altera-produto.php?id=<?=$produto['id']?>">Alterar</a>
</td>
<td>
<input type="hidden" name="id" value="<?=$produto['id']?>">
<button value="<?=$produto['id']?>" type="submit" class="btn btn-warning" data-toggle="modal" data-target="#myModal">Visualizar</button>
</td>
<td>
<form action="deleta-produto.php" method="post">
<input type="hidden" name="id" value="<?=$produto['id']?>">
<button class="btn btn-danger confirmation-callback" onclick="return (window.confirm('Certeza que deseja remover?'));">Remover</button>
</form>
</td>
</tr>
</tbody>
<?php
endforeach;
?>
</table>
<!-- MODAL -->
<?php
$id = $produto['id'];
echo $id;
$produtos = buscaProduto($conexao, $id);
?>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Informaçoes de Produto</h4>
</div>
<div class="modal-body">
Nome: <?= $produtos['nome'] ?><br>
Preço: <?= $produtos['preco'] ?><br>
Cor: <?= $produtos['cor'] ?><br>
Peso: <?= $produtos['peso'] ?><br>
Tamanho: <?= $produtos['tamanho'] ?><br>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Voltar</button>
</div>
</div>
</div>
</div>
<!--FIM MODAL -->
<?php $totalPagina = paginacao($conexao);
echo "<ul class= pagination pagination-large>
<li><a href = consulta-produto.php >Primeira Pagina</a></li>
</ul>";
echo "<ul class= pagination pagination-sm>
<li><a>...</a></li>
</ul>";
for($i = 1; $i <= $totalPagina; $i++){
echo "<ul class= pagination pagination-large>
<li><a href = \"?pagina=$i\">$i</a></li>
</ul>";
}
echo "<ul class= pagination pagination-large>
<li><a>...</a></li>
</ul>";
echo "<ul class= pagination pagination-large>
<li><a href = \"?pagina=$totalPagina\">Ultima Pagina</a></li>
</ul>";
?>
</div>
<?php include ("rodape.php");?>
<?php
?>
I have tried to pass via POST, put the value on the button like this one, but it always brings me the last ID, in case the last of each page as are listed 5 records per page.
If you search the site, you will find a lot of content about the your problem
– Wallace Maxters
Yes, but without success with the solutions presented.
– MHPA
Your line with the id is commented, is it deliberate? What error happens, or what happens ?
– MagicHat
The line with the commented id is purposeful yes.. in fact as you saw there I made a pagination, so I list 5 in 5 records, just when I click on view it calls the div corresponding to the modal so it always passes the last ID of the record, always the last and not the one I click.
– MHPA
And how is your query?
– MagicHat
query selects the product query by the id that should be received by the modal...$query = "select * from product Where id = '{$id}'";
– MHPA
I think I’ve answered a similar question... See if this helps: http://answall.com/questions/130803/passar-id-de-um-dado-de-uma-tabela-para-a-modal-resolvido or http://answall.com/questions/130169/tabela-edit%C3%a1vel-em-php
– Miguel
is exactly the second link what I would like to do, I will try to adapt. thank you
– MHPA
@MHPA Already made modal test without php?
– KingRider
No. I didn’t really understand it, could you explain?? Thank you
– MHPA
@Miguel worked out I got with the second link of the options he gave me put it shows the data in an input.. how to only show without input?
– MHPA
http://jsfiddle.net/Guruprasad_Rao/ooexfj26/
– Jorge B.