Pass varialvel to modal

5

I have a page with a table whose code field is link that opens a modal. I need that when opening the modal, show another table, only with the data according to the value of the field (code) clicked. How do I get the value of the chosen field within the modal

Does anyone know how to do it?

Code:

<table class="table table-bordered table-hover center-text" name="carrtab" id="carrtab"
       style="background-color: #ffffff;">
    <thead align="center">
    <tr class="danger">
        <th class="text-center">Data</th>
        <th class="text-center">Tipo</th>
        <th class="text-center">Descrição</th>
        <th class="text-center">Cliente</th>
        <th class="text-center">Usuario</th>
        <th class="text-center">Link</th>
    </tr>
    </thead>
    <tbody>
    <?

    $tipoentradalog = post('tipoentradalog');
    $usuario = post('usuario');
    $codcliente = post('codcliente');
    $datade = post('datade');
    $dataate = post('dataate');
    $codigo = post('codigo');

        $sql = "select
                    l.datacriacao,
                    tl.nome as tipo,
                    l.descricao,
                    cli.nome as cliente,
                    us.nome as usuario,
                    l.codigo
                    from log l
                    inner join cliente cli on cli.codcliente=l.codcliente
                    inner join usuario us on us.codusuario=l.codusuario
                    inner join empresa emp on emp.codempresa=l.codempresa
                    inner join tipoentradalog tl on tl.codtipoentradalog=l.codtipoentradalog
                    where cli.codempresa=$codempresa
                    and l.datacriacao between '$datade%' and '$dataate%' ";
        if($usuario != ""){
            $sql .= " and us.codusuario=$usuario ";
        }
        if($codcliente != "") {
            $sql .= " and cli.codcliente=$codcliente ";
        }
        if($tipoentradalog != "") {
            $sql .= " and tl.codtipoentradalog=$tipoentradalog ";
        }

        $rst = my_query($connR, $sql);

            foreach($rst as &$row){
                ?>
                <tr>
                    <td align="center"><?=normalDate($row['datacriacao']);?></td>
                    <td align="center"><?=$row['tipo']?></td>
                    <td align="center"><?=$row['descricao']?></td>
                    <td align="center"><?=$row['cliente']?></td>
                    <td align="center"><?=$row['usuario']?></td>
                    <td align="center"><a class="link-target" role="link" data-toggle="modal" href="#modalContainer" aria-expanded="false" aria-controls="modalContainer" style="text-decoration: none; color: black;"><?=$row['codigo']?></a></td>
                </tr>
            <?
                if($row['codigo'] != "")
                {
                    global $codigo;
                    $codigo = $row['codigo'];
                }
            }?>
            </tbody>
        </table>
<div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <h3 class="modal-title" id="myModalLabel">Extrato Rodízio</h3>
                    </div>
                    <div class="modal-body">
                        <h4>Chat</h4>
                        <table name="tabrod" id="tabrod" class="table table-bordered table-hover center-text" style="background-color: #ffffff;">
                            <thead align="center">
                            <tr class="danger">
                                <th class="text-center">Data</th>
                                <th class="text-center">Cliente</th>
                                <th class="text-center">Usuario</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?
                            $sql = "select cli.codcliente, cli.nome, emp.nome as empresa from cliente cli
                                    inner join empresa emp on emp.codempresa=cli.codempresa
                                    where cli.codcliente=$codigo;";
                            $rst = my_query($connR, $sql);

                            foreach ($rst as $row) {
                                ?>
                                <tr>
                                    <td align="center"><?= $row['codcliente']; ?></td>
                                    <td align="center"><?= $row['nome'] ?></td>
                                    <td align="center"><?= $row['empresa'] ?></td>
                                </tr>
                            <?
                            }?>
                            </tbody>
                        </table>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
                    </div>
                </div>

            </div>
    
asked by anonymous 10.08.2015 / 20:28

1 answer

3

On your watch page do so:

<table class="table table-bordered table-hover center-text" name="carrtab" id="carrtab"
   style="background-color: #ffffff;">
<thead align="center">
<tr class="danger">
    <th class="text-center">Data</th>
    <th class="text-center">Tipo</th>
    <th class="text-center">Descrição</th>
    <th class="text-center">Cliente</th>
    <th class="text-center">Usuario</th>
    <th class="text-center">Link</th>
</tr>
</thead>
<tbody>
<?

    $tipoentradalog = post('tipoentradalog');
    $usuario = post('usuario');
    $codcliente = post('codcliente');
    $datade = post('datade');
    $dataate = post('dataate');
    $codigo = post('codigo');

    $sql = "SELECT
                l.datacriacao,
                tl.nome as tipo,
                l.descricao,
                cli.nome as cliente,
                us.nome as usuario,
                l.codigo
            FROM log l
                INNER JOIN cliente cli ON cli.codcliente=l.codcliente
                INNER JOIN usuario us ON us.codusuario=l.codusuario
                INNER JOIN empresa emp ON emp.codempresa=l.codempresa
                INNER JOIN tipoentradalog tl ON tl.codtipoentradalog=l.codtipoentradalog
            WHERE 
                cli.codempresa=$codempresa AND 
                l.datacriacao BETWEEN '$datade%' AND '$dataate%' ";
    if(!empty($usuario)
        $sql .= " and us.codusuario=$usuario ";

    if(!empty($codcliente))
        $sql .= " and cli.codcliente=$codcliente ";

    if(!empty($tipoentradalog))
        $sql .= " and tl.codtipoentradalog=$tipoentradalog ";


    $rst = my_query($connR, $sql);

        foreach($rst as &$row){
            ?>
            <tr>
                <td align="center"><?=normalDate($row['datacriacao']);?></td>
                <td align="center"><?=$row['tipo']?></td>
                <td align="center"><?=$row['descricao']?></td>
                <td align="center"><?=$row['cliente']?></td>
                <td align="center"><?=$row['usuario']?></td>
                <td align="center">
                    <a href="loadmodal.php?codigo=<?=$row['codigo']?>" data-toggle="meumodal" data-target="#modalContainer" class="link-target" role="link" aria-expanded="false" aria-controls="modalContainer" style="text-decoration: none; color: black;"><?=$row['codigo']?></a>
                </td>
            </tr>
    <?php
            if(!empty($row['codigo'])) {
                global $codigo;
                $codigo = $row['codigo'];
            }
        }
    ?>
    </tbody>
</table>

<div class="modal fade" id="modalContainer" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
   <div class="modal-dialog modal-lg">
       <div class="modal-content"></div>
   </div>
</div>

<script type="text/javascript">
$(document).on('click', "[data-toggle='meumodal']", function(event){
  event.preventDefault();
  target = $(this).attr("data-target");
  content = $(this).attr("href");
  $(target+".modal .modal-content").load(content,function(){
     $(target).modal('show');
  });
});
</script>

Note: Notice the table row, where the link is mounted, some changes have been made there. One change worth noting is the href attribute that now has the page name that will be created with the code just below (the target went to the data-target attribute). Also added a script from this answer .

Now create a PHP file that has a connection to your database, such as a completely separate page. And put the following code in it:

<div class="modal-header">
    <h3 class="modal-title" id="myModalLabel">Extrato Rodízio</h3>
</div>
<div class="modal-body">
    <h4>Chat</h4>
    <?php
        if (!empty($_GET['codigo'])){
            $codigo = filter_var($codigo);
    ?>
    <table name="tabrod" id="tabrod" class="table table-bordered table-hover center-text" style="background-color: #ffffff;">
        <thead align="center">
        <tr class="danger">
            <th class="text-center">Data</th>
            <th class="text-center">Cliente</th>
            <th class="text-center">Usuario</th>
        </tr>
        </thead>
        <tbody>
        <?php

            $sql = "SELECT 
                      cli.codcliente, cli.nome, emp.nome as empresa 
                    FROM cliente cli
                       INNER JOIN empresa emp ON emp.codempresa=cli.codempresa
                    WHERE cli.codcliente=$codigo;";
            $rst = my_query($connR, $sql);

            foreach ($rst as $row) {
        ?>
            <tr>
                <td align="center"><?= $row['codcliente']; ?></td>
                <td align="center"><?= $row['nome'] ?></td>
                <td align="center"><?= $row['empresa'] ?></td>
            </tr>
        <?php
            }
        ?>
        </tbody>
    </table>
    <?php 
        } else {
    ?>
        <div class="alert alert-danger" role="alert">
            <p>Oops! O cliente não foi encontrado.</p>
        </div>
    <?php 
        }
    ?>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
    
10.08.2015 / 22:27