【问题标题】:bootstrap modal problems delete data引导模态问题删除数据
【发布时间】:2015-01-19 17:51:19
【问题描述】:

我正在尝试使用引导模式删除确认从我的表中删除一行。 但问题是,当我尝试删除表中的一行时,被删除的是第一行(总是)。为什么会这样?有什么想法吗?

HTML(表格)

<table class="table table-bordered table-hover clearfix" id="example">
<thead>
    <tr>
        <th>ID de Funcionário</th>
        <th>Sobrenome</th>
        <th>Nome</th>
        <th>Cargo</th>
        <th>Telefone</th>
        <th class="text-center">Opção</th>
    </tr>
</thead>
<tbody>
    <?php
        $query="SELECT * FROM employee AS E ORDER BY name";
            $res = mysql_query($query);
            mysql_set_charset('utf-8');
                if(!$res){
                    echo "Erro ao executar a query";
                }
                else{
                    while($dados=mysql_fetch_array($res)){  
                ?>
    <tr>
        <td><?php echo $dados['func_id']; ?></td>
        <td><?php echo $dados['last_name']; ?></td>
        <td><?php echo $dados['name']; ?></td>
        <td><?php echo $dados['position']; ?></td>
        <td><?php echo $dados['phone']; ?></td>
        <td class="text-center">
            <a class="btn btn-danger btn-xs" data-toggle="modal" data-target="#delete" data-whatever="@mdo">
                <span class="fa fa-trash">Apagar</span>
            </a>
            <?php include('modal_delete.php');?>
        </td>
    </tr>
    <?php
    }}
    ?>
</tbody>

Modal_delete.php(包括)

<div class="modal"  id="delete" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog">
    <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 text-left">Apagar</h4>
        </div>
        <div class="modal-body text-left">
            <p>Deseja apagar este  registro?</p>
        </div>
        <div class="modal-footer">
            <a href="../delete/delete_reg.php?id=<?php echo $dados['idemployee'];?>" type="button" class="btn btn-primary">Apagar</a>
            <a type="button" class="btn btn-default" data-dismiss="modal">Cancelar</a>
        </div>
    </div>
</div>

删除_reg.php

<?php
require("../../conection/conection.php");
$id =$_GET["id"];

$query= "DELETE FROM employee WHERE idemployee= $id";
$delete= mysql_query($query);
if(!$delete){
    echo "erro!! não foi possivel apagar dado.";    
}
else{
    echo"Dado removido com sucesso!";
    header("Location: ../list/list_reg.php");
}?>

我在网站上找到的所有可能的解决方案都不能解决我的问题...有人可以帮我吗?谢谢!!!

【问题讨论】:

  • 不要使用mysql_*函数,因为它们是deprecated
  • 好的...谢谢你的信息,我会改变它;)

标签: php html mysql twitter-bootstrap bootstrap-modal


【解决方案1】:

假设您有 5 行。这意味着对于每一行,您都会生成一个模式。这 5 个模态中的每一个都有 id="delete"。当您单击链接打开模态时,引导代码会打开与$('#delete') 匹配的第一个 模态,这意味着它将删除第一行。

您有两种解决方案:

  1. 您在删除按钮的data-target 和模态的ID 中添加echo $dados['idemployee'];。 (不是最优的,因为您仍在为每个表格行生成模态的代码)

  2. 您只制作了 1 个模态,并为删除按钮添加了一个 click() 事件,这会使用正确的 id 更新 Apagar 按钮的 href

【讨论】:

  • 我不知道我是否做错了什么,但没有一个解决方案有效,我会继续尝试:/
  • 如果您使用的是 Chrome 或 Firefox,您可以右键单击并“检查元素”,然后将网页逐个分解。尝试确定它是哪种模式,以及 href 是什么。
  • 好的......第一个解决方案完美运行,现在一切都很好。感谢您的帮助...问题解决了!!! :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-30
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多