Passing PHP-MYSQL values to Modal

Asked

Viewed 105 times

1

I have the following problem, I need to send to Modal data coming from the Database, See below my screen and my code.

            <thead>
                <tr>
                    <th>Foto</th>
                    <th>SKU</th>
                    <th>Nome Produto</th>
                    <th>Categoria</th>
                    <th>Quantidade</th>
                    <th>Fornecedor</th>
                    <th><a href="produtoCadastro.php?id=0" class="btn btn-success btn-sm"><i class="fas fa-shopping-cart"></i>&nbsp;Cadastrar Novo Produto</a></th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <?php
                    include 'conexao.php';
                    $sql = "SELECT * FROM estoque ORDER BY id_estoque";
                    $busca = mysqli_query($conexao, $sql);

                    while ($array = mysqli_fetch_array($busca)) {
                        $id_estoque  =  $array['id_estoque'];
                        $skuproduto  =  $array['skuproduto'];
                        $nomeproduto =  $array['nomeproduto'];
                        $categoria   =  $array['categoria'];
                        $quantidade  =  $array['quantidade'];
                        $fornecedor  =  $array['fornecedor'];

                    ?>
                        <td class="text-center"><button type="button" class="btn btn-primary btn-sm " data-toggle="modal" data-target="#myModal" data-id="<?php echo $id_estoque['id_estoque'];?>" data-nome="<?php echo $nomeproduto['nomeproduto'];?>"><i class="far fa-image"></i></button></td>
                        <td><?php echo $skuproduto  ?></td>
                        <td><?php echo $nomeproduto ?></td>
                        <td><?php echo $categoria   ?></td>
                        <td><?php echo $quantidade  ?></td>
                        <td><?php echo $fornecedor  ?></td>

                        <td>
                            <a class="btn btn-warning btn-sm" style="color:white" href="editarProduto.php?id=<?php echo $id_estoque ?>" role="button"><i class="far fa-edit"></i>&nbsp;Editar</a>
                            <?php echo ("<a class='btn btn-danger btn-sm' style='color:white' href='javascript:excluirProduto($id_estoque)' role='button'><i class='far fa-trash-alt'></i></i>&nbsp;Excluir</a>") ?>
                        </td>
                </tr>
            <?php } ?>
            </tbody>

        </table>

    </div>

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                
                <div class="modal-header">                    
                    <h4><?php echo $skuproduto ?></h4>                    
                </div>
               

                <div class="modal-body">
                    <img src="img/logo.png"" class=" rounded mx-auto d-block" alt="..." style="width:200px">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Fechar</button>
                </div>
            </div>
        </div>
    </div>

<script type="text/javascript">
    $('#myModal').on('show.bs.modal', function (event) {                                                      
        var button = $(event.relatedTarget) 
        var recipientId    = button.data('id')                                                                 
        var recipientNome = button.data('nome') 
        var modal = $(this)
        modal.find('#id').val(recipientId) 
        modal.find('#nome').val(recipientNome)
    })
</script>

Essa é minha tela, onde possuo um cadastro de produtos

This is my screen, where I have a product registration, the photo button opens my modal, in it I need to recover the product id, product name and product photo. Remembering that I have not yet prepared my bank for the registration of the photo

modal

So in my modal I need to bring the photo along with the product name, currently my code is searching data from the last record of my database.

I appreciate the help.

  • What values do you want to pass to the modal? Are they already in your table? If you are already the problem is one, but if you need to do a new database query the problem is completely different.

  • I want to pass the product name and the photo. The product name is already in my table but the photo is not yet. The way I did it is returning the last record of my bank and I need it to bring the record of what I clicked

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.