PHP- How to pass an id through a modal window

Asked

Viewed 40 times

-1

I’m not getting an id to a modal window. Could help me?

I’m using a table, which lists all the names and CNPJ’s of registered customers.

I created a loop, to show in each row of the table, the change and delete buttons.

The change button works perfectly, but the delete button (which is calling the modal to confirm client deletion, is only taking the id of the first client from the table, and not the selected client).

Follow the page code with table and modal:

include ".. /connection.php";

$command = "Select * from tbclient";

if (!Empty($pesq)) { $command .= " Where razao_social LIKE '%" . $pesq . " %' "; }elseif(!Empty($pesq2)){ $command .= " Where cnpj = $pesq2"; }

$command .= " order by razao_social";

$sql = mysqli_query($connect, $command);

?>

Customer Consultation

<div style="overflow: auto; width: 100%; height:480px; max-height: 480px;"> 


<table style="width: 80%; background-color: #fff" align="center" border="0">

    <tr>
        <th style="width: 50%; background-color: #2e4e8e; color: #fff;">
            <form action='clienteconsulta.php' method="GET" >
                Nome<br><br>
                <input type="text" name='pesquisa' value='<?php echo $pesq ?>'style="height: 30px; color:#000;" >
        </th>
        <th style="background-color: #2e4e8e;color: #fff;">
            CNPJ<br><br>
            <input type="text" name='consultacnpj' value='<?php echo $pesq2 ?>' style="height: 30px; color:#000;">
        </th>
        <th style="background-color: #2e4e8e;color: #fff;">&nbsp;<br><br>
            <button input type="submit" class="btn btn-primary">Buscar</button>
        </th>
        <th style="background-color: #2e4e8e;color: #fff;">&nbsp;</th>
            </form>
    </tr>

    <?php
    
    while ($result = mysqli_fetch_array($sql)) {
        ?>
        <tr align="center">
            <td><?php echo $result['RAZAO_SOCIAL']; ?></td>
            <td><?php echo $result['CNPJ'] . "&nbsp"; ?></td>
            <td>
                <a style="text-align: center;" href="clientealtera.php?id=<?php echo $result['IDCLIENTE']; ?>">
                    <span class="">Alterar</span><br>
                </a>
            </td>
            <td>
                <a href="" data-toggle="modal" data-target="#ExemploModalCentralizado">
                    Excluir
                </a>
                <div class="modal fade" id="ExemploModalCentralizado" tabindex="-1" role="dialog" aria-labelledby="TituloModalCentralizado" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h4 class="modal-title" align="left"><strong>Confirmação de exclusão</strong></h4>
                            </div>

                            <div class="modal-body" align="left">
                                Deseja realmente excluir o cliente selecionado?
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
                                <a href="clientedelete.php?id=<?php echo $result['IDCLIENTE']; ?>"><button type="submit" class="btn btn-primary">Confirmar</button> </a>
                            </div>
                        </div>
                    </div>
                </div>
            </td>
        </tr>
    <?php } ?>
</table>
</div>

NOTE: If you put the function href="clientedelete.php? id=" inside the delete button outside the modal, it takes the correct id for deletion.

1 answer

0


This is happening due to your code organization, ids cannot repeat themselves, in your loop when you click to delete the third itém, for example, in fact you are calling the first modal of the listing. Because they have the same call selector id="ExemploModalCentralizado", you can solve this by adding an auto increment variable $i++; right after you open your while and concatenate it with modal id, so you create unique ids for each modal.

But I make an observation, as you are using bootstrap there are ways for you to use only one modal and pass the values you need according to the clicked line, give a look at the end of the modal documentation of the bootstrap, https://getbootstrap.com/docs/4.0/components/modal/#Varying-modal-content

...data-target="#ExemploModalCentralizado-<?php echo $i; ?>"...
...id="ExemploModalCentralizado-<?php echo $i; ?>"...

The whole code would look like this:

<div style="overflow: auto; width: 100%; height:480px; max-height: 480px;"> 


<table style="width: 80%; background-color: #fff" align="center" border="0">

    <tr>
        <th style="width: 50%; background-color: #2e4e8e; color: #fff;">
            <form action='clienteconsulta.php' method="GET" >
                Nome<br><br>
                <input type="text" name='pesquisa' value='<?php echo $pesq ?>'style="height: 30px; color:#000;" >
        </th>
        <th style="background-color: #2e4e8e;color: #fff;">
            CNPJ<br><br>
            <input type="text" name='consultacnpj' value='<?php echo $pesq2 ?>' style="height: 30px; color:#000;">
        </th>
        <th style="background-color: #2e4e8e;color: #fff;">&nbsp;<br><br>
            <button input type="submit" class="btn btn-primary">Buscar</button>
        </th>
        <th style="background-color: #2e4e8e;color: #fff;">&nbsp;</th>
            </form>
    </tr>

    <?php
    
    while ($result = mysqli_fetch_array($sql)){
        $i++;
        ?>
        <tr align="center">
            <td><?php echo $result['RAZAO_SOCIAL']; ?></td>
            <td><?php echo $result['CNPJ'] . "&nbsp"; ?></td>
            <td>
                <a style="text-align: center;" href="clientealtera.php?id=<?php echo $result['IDCLIENTE']; ?>">
                    <span class="">Alterar</span><br>
                </a>
            </td>
            <td>
                <a href="" data-toggle="modal" data-target="#ExemploModalCentralizado-<?php echo $i; ?>">
                    Excluir
                </a>
                <div class="modal fade" id="ExemploModalCentralizado-<?php echo $i; ?>" tabindex="-1" role="dialog" aria-labelledby="TituloModalCentralizado" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h4 class="modal-title" align="left"><strong>Confirmação de exclusão</strong></h4>
                            </div>

                            <div class="modal-body" align="left">
                                Deseja realmente excluir o cliente selecionado?
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
                                <a href="clientedelete.php?id=<?php echo $result['IDCLIENTE']; ?>"><button type="submit" class="btn btn-primary">Confirmar</button> </a>
                            </div>
                        </div>
                    </div>
                </div>
            </td>
        </tr>
    <?php } ?>
</table>
</div>
  • 1

    It worked out! Thank you very much!

Browser other questions tagged

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