I can not show and delete a record using a pop-up window

2

The delete function works perfectly by doing the following with the delete button.

<a href="borrar.php?id=<?php echo $persona->id; ?>"><button type="button" class="btn btn-danger" onClick="<?php $identificador = $persona->id; ?>">Eliminar</button></a>

As you may notice, I send a parameter to my file delete.php

<?php
require('conexion.php');
$id = $_GET['id'];
try{
    $sql="delete from datos_usuarios where id='$id'";
    $execute = $base->query($sql);
    header ('Location: index.php');

}catch(Exception $e){
    die ('Error no se ah podido eliminar el usuario' . $e->getMessage());
}

Seeing the simplicity of my program, I decided to add a modal bootstrap in which the information of the person I want to eliminate and two buttons is displayed, one to confirm that he is sure to delete the record and the other to cancel said action.

The problem is that I can not find the way to show the information of the person that was selected, it occurred to me that when I clicked on the delete button I stored the id of the record I was trying to delete in the following way. :

<a href="#ventana1" data-toggle="modal"><button class="btn btn-danger btn-xs" onClick="$identificador = $persona->id; ?>" >Eliminar</button></a>

As you can understand, I am a newbie in php and I do not really see any error but I think it has to do with not assigning the value to the variable, well then, without a hug, I enclose all my code. index.php file

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title></title>

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">

  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.2/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
  <![endif]-->
</head>
<body>
<?php

include ('conexion.php');

$registros  = $base->query('SELECT * FROM DATOS_USUARIOS')->fetchAll(PDO::FETCH_OBJ);

?>
<h3 class="text-center">Registro de usuarios</h3>
  <div class="container">

      <!--VENTANA MODAL -->

      <div class="modal fade" id="ventana1" tabindex="-1" role="dialog" aria-labelledby="" aria-hidden="true">
          <div class="modal-dialog">
              <div class="modal-content">
                  <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                      <h4 class="modal-title" id="">Eliminar usuario</h4>
                  </div>
                  <div class="modal-body">
                      <?php
                      $resultado = $base->query("select * from datos_usuarios where id='$identificador' limit 1")->fetchAll(PDO::FETCH_OBJ);
                      foreach($resultado as $rs){
                          echo "Esta seguro que desea eliminar el usuario <b>" . $rs->nombre . "</b>";
                      }

                      ?>

                  </div>
                  <div class="modal-footer">
                      <a href="borrar.php?id=<?php echo $identificador; ?>"><button type="button" class="btn btn-danger" data-dismiss="">Eliminar</button></a>
                      <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                  </div>
              </div>
          </div>
      </div>
      <!--END MODAL -->

    <div class="row">
      <div class="col-lg-8">
        <div class="panel panel-default">
          <div class="panel-heading">Formulario de registro</div>
          <div class="panel-body">

            <div class="table table-responsive text-center">
              <table class ="table table-bordered table-condensed table-striped table-responsive table-hover">
                <thead>
                  <th>ID</th>
                  <th>Nombre</th>
                  <th>Apellido</th>
                  <th>Direccion</th>
                  <th>Opciónes</th>
                </thead>
                  <?php
                  foreach($registros as $persona):?>

                <tr>
                    <td><?php echo $persona->id?></td>
                    <td><?php echo $persona->nombre?></td>
                    <td><?php echo $persona->apellido?></td>
                    <td><?php echo $persona->direccion?></td>
                    <td>
                        <a href="actualizar.php"><button class="btn btn-primary btn-xs">Actualizar</button></a>
                        <a href="#ventana1" data-toggle="modal"><button class="btn btn-danger btn-xs" onClick="$identificador = $persona->id; ?>" >Eliminar</button></a>
                        <!-- <a href="borrar.php?id=<?php echo $persona->id; ?>"><button type="button" class="btn btn-danger" onClick="<?php $identificador = $persona->id; ?>">Eliminar</button></a> -->
                    </td>

                </tr>
                  <?php
                  endforeach;
                    ?>
                <tr>
                  <td></td>
                  <td><input type="text" name="" value=""></td>
                  <td><input type="text" name="" value=""></td>
                  <td><input type="text" name="" value=""></td>
                    <td><div class="text-center">
                            <button type="button" name="button" class="btn btn-success btn-xs">Insertar</button>

                        </div></td>
                </tr>
              </table>


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

      </div>

    </div>
  </div>

</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</body>
</html>
    
asked by jeancarlos733 27.02.2017 в 08:49
source

1 answer

1

You have to include that information in an html that will be shown when you press that button, there you can define the information you want and the actions of the buttons.

Check the documentation page.

link

You write the html of the modal somewhere on the page and on the button you indicate which is with data-target="# myModal".

In your case you would have to write a modal for each entry in the table. Another alternative would be to query that data dynamically with ajax and populate the modal dynamically, in which case you would only have to write an empty modal in html.

Reading your code it seems that you want to run the php after processing it, clicking from the browser, and that is not possible, you need to do it with javascript.

Put the html code of the modal inside the loop and assign each modality an id, which corresponds to the data-target parameter of each button.

    
answered by 27.02.2017 в 09:42