如何使用php+html+ajax删除一行?

vcirk6k6  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(443)

我一直在尝试从html表中删除一行。我用的是物化css
这是我得到的,id是员工id

这是密码

<?php 
  $server = mysql_connect("localhost", "root", ""); 
  mysql_set_charset('utf8', $server);
  $db = mysql_select_db("cursos", $server);
  $query = mysql_query("SELECT * FROM empleados"); 
?>

         <table class="bordered responsive-table highlight">
             <thead>
            <tr>
                <th>ID</th>
                <th>Nombre</th>
                <th>Apellidos</th>
                <th>Cumpleaños</th>
                <th>Género</th>
                <th>Nacionalidad</th>
                <th>Despedir</th>
            </tr>
</thead>
            <?php
               while ($row = mysql_fetch_array($query)) {
?>
             <tbody>
                    <tr>
                        <td><?php echo $row['empleado_id']; ?></td>
                        <td><?php echo $row['nombre']; ?></td>
                        <td><?php echo $row['apellidos']; ?></td>
                        <td><?php echo $row['fecha_nac']; ?></td>
                        <td><?php echo $row['genero']; ?></td>
                        <td><?php echo $row['nacionalidad']; ?></td>
                            <?php

                            $empleado_id = $row['empleado_id'];                         
                                    ?>
                        <td><a id="<?php echo $empleado_id; ?>" name="borrar" class="btn-floating btn-small waves-effect waves-light red"><i class="material-icons">delete</i></a></td>
                    </tr>
             </tbody>
    <?php
               }

            ?>
        </table>

我希望能够单击红色按钮并将查询发送到sql。
到目前为止,这就是我所拥有的

<td><a id="<?php echo $empleado_id; ?>" name="borrar" class="btn-floating btn-small waves-effect waves-light red"><i class="material-icons">delete</i></a></td>

通过使用 id="<?php echo $empleado_id; ?>" ,红色按钮的id,它总是雇员id。
如何将按钮id传递给执行查询的函数?

kpbpu008

kpbpu0081#

在a标记中设置onclick属性,如下所示:

<td><a onClick="deletThis(this.id)" id="<?php echo $empleado_id; ?>" name="borrar" class="btn-floating btn-small waves-effect waves-light red"><i class="material-icons">delete</i></a></td>

现在在script标记中的文件末尾,执行以下函数

<script>
   function deletThis(employeeId){
      //for surity if the value ir right u can chech in console
      console.log(employeeId);
      //now make an AJAX request to your file let's say delete.php
      $.post("./delete.php",
         {
           employeeId : employeeId
         },
         function(response, status){
             console.log(response);
         });
      }
</script>

现在,将delete.php文件与此文件并行,并编写php代码来删除条目,如下所示

<?php

   $employeeId = $_POST['employeeId'];

   //delte from db

?>

相关问题