【问题标题】:pop up confirmation delete for php弹出确认删除php
【发布时间】:2019-10-02 18:13:21
【问题描述】:

所以我的代码在下面,它从 dmgeqdelete.php 读取,它将立即从数据库中删除数据。 <td align="center"> <a href="dmgeqdelete.php?damagedID=<?php echo $row["damagedID"]; ?>" >Delete</a> </td>

我正在尝试为上述代码添加弹出确认。我试过<td align="center"> <a href="dmgeqdelete.php?damagedID=<?php echo $row["damagedID"]; ?>" onclick="return confirm('Are you sure?')">Delete</a> </td> 最终没有发生任何动作

dmgeqdelete.php

<?php
include('dbConfig.php');
$damagedID=$_REQUEST['damagedID'];
$query = "DELETE FROM damagedeq WHERE damagedID=$damagedID"; 
$result = mysqli_query($conn,$query) or die ( mysqli_error());
header("Location: dmgeqview.php"); 
?>

完整代码

<?php
include('dbConfig.php');
?>
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>View Damaged Equipment Records</title>
  <link rel="stylesheet" href="css/style.css" />
</head>

<body>
  <div class="form">
    <p><a href="report.php">Home</a> | <a href="index.php">Logout</a></p>
    <h2>View Records</h2>
    <table width="100%" border="1" style="border-collapse:collapse;">
      <thead>
        <tr>
          <th><strong>No</strong></th>
          <th><strong>Damage ID's</strong></th>
          <th><strong>EQ ID's</strong></th>
          <th><strong>Student ID's</strong></th>
          <th><strong>Staff ID's</strong></th>
          <th><strong>Date reported</strong></th>
          <th><strong>Date repaired</strong></th>
          <th><strong>Equipment Status</strong></th>
        </tr>
      </thead>
      <tbody>
        <?php
$count=1;
$sel_query="Select * from damagedeq ORDER BY damagedID desc;";
$result = mysqli_query($conn,$sel_query);
while($row = mysqli_fetch_assoc($result)) { ?>
          <tr>
            <td align="center">
              <?php echo $count; ?>
            </td>
            <td align="center">
              <?php echo $row["damagedID"]; ?>
            </td>
            <td align="center">
              <?php echo $row["equipmentID"]; ?>
            </td>
            <td align="center">
              <?php echo $row["studentID"]; ?>
            </td>
            <td align="center">
              <?php echo $row["staffID"]; ?>
            </td>
            <td align="center">
              <?php echo $row["dateReported"]; ?>
            </td>
            <td align="center">
              <?php echo $row["dateRepaired"]; ?>
            </td>
            <td align="center">
              <?php echo $row["equipmentStatus"]; ?>
            </td>
            <td align="center">
              <a href="dmgequp.php?damagedID=<?php echo $row[" damagedID "]; ?>">Edit</a>
            </td>
            <td align="center">
              <a href="dmgeqdelete.php?damagedID=<?php echo $row[" damagedID "]; ?>">Delete</a>
            </td>
          </tr>
          <?php $count++; } ?>
      </tbody>
    </table>
  </div>
</body>

</html>

【问题讨论】:

  • 使用实际的单击侦听器函数来处理确认,然后使用 ajax 将数据发布到 php,并在 php 中使用准备好的语句的 PDO。

标签: javascript php html database xampp


【解决方案1】:

这里只需要停止代码的进一步执行

onclick="return confirm('Are you sure?')"

像这样在末尾添加return false

onclick="return confirm('Are you sure?'); return false;"

当点击cancel时什么都不会发生,但是如果点击ok它将转到链接
这是完整的代码sn-p

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>View Damaged Equipment Records</title>
  <link rel="stylesheet" href="css/style.css" />
</head>

<body>
  <div class="form">
    <p><a href="report.php">Home</a> | <a href="index.php">Logout</a></p>
    <h2>View Records</h2>
    <table width="100%" border="1" style="border-collapse:collapse;">
      <thead>
        <tr>
          <th><strong>No</strong></th>
          <th><strong>Damage ID's</strong></th>
          <th><strong>EQ ID's</strong></th>
          <th><strong>Student ID's</strong></th>
          <th><strong>Staff ID's</strong></th>
          <th><strong>Date reported</strong></th>
          <th><strong>Date repaired</strong></th>
          <th><strong>Equipment Status</strong></th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td align="center">000</td>
          <td align="center">111</td>
          <td align="center">222</td>
          <td align="center">333</td>
          <td align="center">444</td>
          <td align="center">555</td>
          <td align="center">666</td>
          <td align="center">777</td>
          <td align="center">
            <a href="dmgequp.php?damagedID=aaa">Edit</a>
          </td>
          <td align="center">
            <a href="dmgeqdelete.php?damagedID=bbb" onclick="return confirm('Are you sure?'); return false;">Delete</a>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</body>

</html>

【讨论】:

  • 感谢您的解决方案,所以基本上我需要停止进一步执行代码 href="dmgeqdelete.php?damagedID="
  • 不,您不需要返回额外的 false。单击取消/否已返回 false。您可能在第一次尝试时有一些错字。请记住,这是非常糟糕的做法,永远不要使用 GET 变量来操作任何东西。这将使您的数据库清空。见这里:https://jsfiddle.net/ou0szc3r/
  • 很难说,因为 OP 没有包含他使用它的原始代码,只引用了它。唯一要学习的是confirm返回truefalse,触发后什么也没有,正如你从jsfiddle看到的那样。您甚至可以从您的 sn-p 中删除 return false; 并注意它没有任何区别。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多