How to Recover Variable Value in a Bootstrap Modal Window

Asked

Viewed 707 times

1

My problem is this: I have this code that displays several cars on a table:

<?php
$veiculos = "SELECT * from veiculo ";
$veiculos .= "WHERE id_secretaria = '1' ORDER BY placa DESC LIMIT 2"; //mudar conforme o id da secretaria
$query_veiculos = mysqli_query($conexao,$veiculos);
if(!$query_veiculos) {
die("Falha na consulta ao banco");
}
while ( $exibir = mysqli_fetch_array($query_veiculos)) {
$placa = $exibir["placa"];
?>
<li><a href="#" data-toggle="modal" data-target="#mostrar-veiculo" data-id='$placa' id="btnEditar"><?php  echo "Modelo: ".$exibir["modelo"]." | Placa: ".$placa." ";?><i class="ti-car pull-right"></i></a></li>
<?php } ?>

Clicking on the link opens a modal with the following code:

<?php
include('../conecta-db.php');

$placa = $_POST["placa"];

$veiculos = "SELECT * from veiculo ";
$veiculos .= "WHERE placa = '{$placa}'";
$query_veiculos = mysqli_query($conexao,$veiculos);
if(!$query_veiculos) {
die("Falha na consulta ao banco");
}

?>


<!-- Modal -->
<div class="modal fade" id="mostrar-veiculo" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
  <div class="modal-header">
    <h5 class="modal-title" id="exampleModalLabel">Veiculo selecionado</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
     <!--Lista com os cadastros-->
<div class="row">
  <div class="col-md-12">
  <div class="container">


  <table class="table table-responsive">
    <thead class="bg-primary">
      <tr style="color: #fff;">
        <th scope="col">#</th>
        <th scope="col">Placa</th>
        <th scope="col">Modelo</th>
        <th scope="col">Ano</th>
        <th scope="col">Km Rodados</th>
      </tr>
    </thead>
    <tbody>
      <?php $i = 1;?>

      <?php 
        while ( $exibir = mysqli_fetch_array($query_veiculos)) {
      ?>
      <tr>
        <th scope="row"><?php echo $i; ?></th>
        <td><?php echo $exibir["placa"]; ?></td>
        <td><?php echo $exibir["modelo"]; ?></td>
        <td><?php echo $exibir["ano"]; ?></td>
        <td><?php echo $exibir["km_rodados"]; ?></td>
      </tr>
    <?php $i++;} ?>
    </tbody>
  </table>
</div>
  </div>
</div>
  <div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal" id="botao-fechar" style="width: 90%;
display: table;
margin-left: auto;
margin-right: auto;
position: static;">Fechar</button>
  </div>
</div>

I want to know how I can recover the value of the card variable in the modal to call only the line that has the same card in the database. follow system images:

Here the links of the cars: links

Here the modal where you should display all car information: modal

  • Is an ajax?.....

  • And this table Carros desta secretária (ou seria secretaria?) what’s her deal? It just came up in the middle of the question

1 answer

0


You can do like me, it’s kind of a gambit, but it served me. Just put that code inside a function and call it by an onClick().

$('#idDaModal').modal();
$("#idDoInput").val(variavalAqui);

Then you just create an input with type = Hidden that will receive this value. I don’t know if I got it right, but I hope I’ve helped.

  • Unfortunately it didn’t work =(

  • Marlon, take a look at this code here: http://jsfiddle.net/o5fa6k7L/ it does what I told you upstairs, if it meets your needs implement it to your code

  • Poxa helped a lot, thank you!

  • You’re welcome, Marlon

Browser other questions tagged

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