首页 > 解决方案 > 引导模式没有获取用户 ID

问题描述

我对引导模式有疑问。我正在使用 PHP。

在管理面板中有一个表格,其中包含用户列表以及编辑或删除用户个人资料的可能性。对于删除,我想创建一个模式来确认删除,但是,当我点击模式内的“确认”按钮时,模式默认获取表中第一个用户的用户 ID,而不是用户 ID选定的用户。

这是代码:

<?php foreach ($utenti as $utente) { ?>
<tr>

  <th scope="row"> <?php echo $utente['idUser']?> </th>

  <td><?php echo $utente['nome']." ".$utente['cognome']?></td>
  <?php if($_SESSION['role'] == 1) {?>
  <td><?php echo $utente['az']?></td>
  <?php } ?>

  <td><?php echo $utente['email']?></td>

  <td class="text-warning"><a
    href="<?php echo 'editUser.php?user='.$utente['idUser']?>"><i
    class="fas fa-edit text-warning"></i></a></td>
  <!--    <td class="text-warning"><a href="  "><i class="fas fa-trash-alt text-danger"></i></a></td> -->
  <td class="text-danger">
    <button type="button" class="btn" data-toggle="modal"
            data-target="#confirmDelete"><i
      class="fas fa-trash-alt text-danger"></i><?php var_dump($utente['idUser']); ?>
    </button>
  </td>

  <div class="modal" tabindex="-1" role="dialog" id="confirmDelete">

    <div class="modal-dialog" role="document">

      <div class="modal-content">

        <div class="modal-header">
          <h5 class="modal-title">
            Attenzione <?php var_dump($utente['idUser']); ?></h5>

          <button type="button" class="close" data-dismiss="modal"
                  aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>

        <div class="modal-body">
          <p>Continuando eliminerai l'utente in maniera irreversibile</p>
        </div>

        <div class="modal-footer">
          <button type="button" class="btn btn-danger "
          ><a
            class="text-white btn-modal-confirm"
            href="<?php echo '?action=delete&user='.$utente['idUser']?>"
          >Elimina</a>
          </button>

          <button type="button" class="btn btn-secondary" data-dismiss="modal">
            Indietro
          </button>
        </div>

      </div>

    </div>

  </div>

</tr>

<?php }?>

如果我在模态之前进行 var_dump $utente['idUser'],它将获得正确的用户 ID。正如我所说,如果我在模态中创建它,默认情况下它会获得第一个 ID。

标签: phptwitter-bootstraphtml-tablemodal-dialog

解决方案


请注意,每个模态触发按钮都有一个data-target属性来定义将打开哪个模态。

在您的情况下,您用来触发模式的每一行的按钮都具有相同的data-target,即#confirmDelete. 后面的这些模态也有相同的id调用#confirmDelete,所以每次你点击模态触发按钮(都有相同的data-target)然后最终它会显示第一个模态元素。

为了更好地理解,请将我的代码与您的代码进行比较并查看差异。

<?php foreach ($utenti as $utente) { ?>
<tr>

  <th scope="row"> <?php echo $utente['idUser']?> </th>

  <td><?php echo $utente['nome']." ".$utente['cognome']?></td>
  <?php if($_SESSION['role'] == 1) {?>
  <td><?php echo $utente['az']?></td>
  <?php } ?>

  <td><?php echo $utente['email']?></td>

  <td class="text-warning"><a
    href="<?php echo 'editUser.php?user='.$utente['idUser']?>"><i
    class="fas fa-edit text-warning"></i></a></td>
  <!--    <td class="text-warning"><a href="  "><i class="fas fa-trash-alt text-danger"></i></a></td> -->
  <td class="text-danger">
    <button type="button" class="btn" data-toggle="modal"
            data-target="#confirmDelete_<?php echo $utente['idUser']; ?>"><i
      class="fas fa-trash-alt text-danger"></i><?php var_dump($utente['idUser']); ?>
    </button>
  </td>

  <div class="modal" tabindex="-1" role="dialog" id="confirmDelete_<?php echo $utente['idUser']; ?>">

    <div class="modal-dialog" role="document">

      <div class="modal-content">

        <div class="modal-header">
          <h5 class="modal-title">
            Attenzione <?php echo $utente['idUser']; ?></h5>

          <button type="button" class="close" data-dismiss="modal"
                  aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>

        <div class="modal-body">
          <p>Continuando eliminerai l'utente in maniera irreversibile</p>
        </div>

        <div class="modal-footer">
          <button type="button" class="btn btn-danger "
          ><a
            class="text-white btn-modal-confirm"
            href="<?php echo '?action=delete&user='.$utente['idUser']?>"
          >Elimina</a>
          </button>

          <button type="button" class="btn btn-secondary" data-dismiss="modal">
            Indietro
          </button>
        </div>

      </div>

    </div>

  </div>

</tr>

<?php }?>

在上面的代码中,我给每对模态元素(模态触发按钮和模态 ID)一个唯一的data-target值和一个唯一的元素 ID。

...
<button type="button" class="btn" data-toggle="modal"
            data-target="#confirmDelete_<?php echo $utente['idUser']; ?>">
...
<div class="modal" tabindex="-1" role="dialog" id="confirmDelete_<?php echo $utente['idUser']; ?>">
...

现在每对模态元素都有自己的ids,它们应该按照您想要的方式工作。


推荐阅读