php – How to update selected record via Modal window?

Question:

My question is the following

If I have a table that lists the database records

HTML part

  <table id="tablaDatosMain" class="table table-bordered table-hover">
    <thead>
       <tr>
          <th>NIT</th>
          <th>Nombre</th>
          <th>Teléfono</th>
          <th></th>                            

       </tr>
    </thead>

    <tbody>
       //Aquí va la consulta que trae todos los registros de la tabla proveedores
      <?php while(...) {?>
      <tr>
         <td><?php echo $fila["NITCLIENTE"]; ?></td>
         <td><?php echo $fila["CLIENTE"]; ?></td>
         <td><?php echo $fila["TELEFONO"]; ?></td>

         <td>

          <div class="btn-group">
            <button type="button" class="btn btn-default">Opciones</button>
            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
            <span class="caret"></span>
            <span class="sr-only">Desplegar</span>
            </button>
            <ul class="dropdown-menu" role="menu">
              <li><a href="#" data-toggle="modal" data-target="#myModal">Modificar</a></li>

              <li><a href="#" onclick="confirmar('dist/rest/rest_action.php?opc=7&i=<?php echo $fila["ID"]; ?>'); return false;">Eliminar</a></li>
            </ul>
          </div>
        </td>
      </tr>
     //Este modal esta dentro del ciclo while que itera las filas con registros
        <?php include('dist/includes/modals/md_prov.php'); ?>
     <?php }; ?>
    </tbody>
    <tfoot>
       <tr>
         <th>NIT</th>
         <th>Nombre</th>
         <th>Teléfono</th>

         <th></th>
       </tr>
   </tfoot>
 </table>

How to make it so that when pressing modify, the modal window brings me the information of the record that I am pressing and not only the first record of the row?

PHP part

if($opc == '6'){                                                                  
//MODIFICAR ITEM DE TABLA DE PROVEEDORES



    $idProveedor = $_GET['i'];
    $usuario = $_SESSION['usuario'];
    $fecha = date('Y-m-d H:i:s');
    $txtTelefono = mysqli_real_escape_string($link,$_POST['txtTelefono2']);
    $txtNombre = mysqli_real_escape_string($link,$_POST['txtNombre2']);
    $txtUser = $_POST['txtUser'];
    $fecha = date('Y-m-d H:i:s');


    $sqlModifica = "UPDATE acsi_posterceros SET CLIENTE = '$txtNombre', TELEFONO = '$txtTelefono', AUDIT_FECHA = '$fecha', AUDIT_USER = '$txtUser' WHERE ID = '$idProveedor' AND ESPROVEEDOR = 1 ";


    if(mysqli_query($link,$sqlModifica)){

        header('Location: ../../p_proveedores.php?opc=OKact');


    }else{
        $err = mysqli_error($link);
        header('Location: ../../p_proveedores.php?opc=NOact&er='.$err);
    }


}

Boostrap modal

<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">
       <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title" id="myModalLabel">Actualizar datos del <strong> Proveedor. </strong></h4>
     </div>

     <div class="modal-body">
       <div class="row">
         <div class="col-xs-12">
          <div class="box box-primary">
            <div class="box-header">
              <h3 class="box-title">Modifique los siguiente datos para <strong> Actualizar </strong> Proveedor</h3>

            </div>
              <!-- /.box-header -->

           <form role="form" name="frmActualizaProv" method="post" action="dist/rest/rest_action.php?opc=6&i=<?php echo $fila["ID"]; ?>">
                  <div class="box-body">
                      <div class="col-md-6 col-xs-12">
                          <div class="form-group">
                              <i class="fa fa-asterisk" style="color: #f56954;" title="Campo Requerido"></i>&nbsp;&nbsp;
                              <label for="txtNit">NIT</label>
                              <input readonly="" type="text" class="form-control" id="txtNit" name="txtNit" placeholder="Introduzca el Nit del nuevo Cliente" required="" tabindex="1" value="<?php echo $fila["NITCLIENTE"]; ?>">
                          </div>
                          <div class="form-group">
                              <i class="fa fa-asterisk" style="color: #f56954;" title="Campo Requerido"></i>&nbsp;&nbsp;
                              <label for="txtTelefono">Teléfono</label>
                              <input type="text" class="form-control" id="txtTelefono" name="txtTelefono2" placeholder="Introduzca el número de Teléfono del nuevo Cliente" required="required" tabindex="3" value="<?php echo $fila["TELEFONO"]; ?>">
                          </div>
                      </div>
                      <div class="col-md-6 col-xs-12">
                          <div class="form-group">
                              <i class="fa fa-asterisk" style="color: #f56954;" title="Campo Requerido"></i>&nbsp;&nbsp;
                              <label for="txtNombre">Nombre</label>
                              <input type="text" class="form-control" id="txtNombre" name="txtNombre2" placeholder="Introduzca el Nombre del nuevo Cliente" required="required" tabindex="2" value="<?php echo $fila["CLIENTE"]; ?>">
                          </div>
                     </div>  
                  </div>
                  <!-- /.box-body -->
                  <input type="hidden" name="txtUser" value="<?php print $_SESSION['usuario']; ?>">
                  <div class="box-footer">
                      <button style="margin-right: 5px;" type="submit" class="btn btn-success pull-right">Actualizar</button>
                      <button style="margin-right: 5px;" type="reset" class="btn btn-default pull-right" data-dismiss="modal">Cancelar</button>
                  </div>
                  <!-- /.box-footer -->
              </form>

          </div>
      </div>
  </div>
  </div>

</div>

Thanks in advance for your help, I don't know how else to summarize them so that you understand me…

Answer:

Well, what I did to solve it was the following: in the HTML part, where the cycle that iterates the providers starts, I initialized a counter in this way.

while ($fila = mysqli_fetch_array($resBuscaProveedores, MYSQLI_BOTH)){ $i=0;?>
 <?php include('dist/includes/modals/md_prov.php'); ?>
   //AQUI VA EL BODY DE LA TABLA CON LOS <td> <?php echo $fila[];?> </td>
 <?php $i++; ?>
<?php } ?>

In the options part, in the modify button, I add the record row and the counter to the data target, just so that each record brings a modal with different information.

<a href="#" data-toggle="modal" data-target="#myModal<?php echo 
  $fila["ID"][$i]; ?>">Modificar</a>

In the Modal part I did this to make it match the record that is being clicked on to modify.

<div class="modal fade" id="myModal<?php echo $fila["ID"][$i]?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" >

And further down the form within the modal I control as a parameter the current row that is iterating.

<form role="form" name="frmActualizaProv" method="post" action="dist/rest/rest_action.php?opc=6&i=<?php echo $fila["ID"][$i]?>">
Scroll to Top