如何在表中放置链接

ttcibm8c  于 2021-08-13  发布在  Java
关注(0)|答案(3)|浏览(343)

我的网站上有一个表,它从数据库中获取数据,在表中我有一行的delete按钮,我刚刚用jquery使表可以搜索,但是现在我不知道如何保留delete按钮。
索引.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta charset="utf-8">
    <meta name="description" content="Inventar">
    <meta name="author" content="Martin Eide Bjørndal">
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="/src/css/style.css">
       <!-- jQuery library -->
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <!-- Popper JS -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
        <!-- Latest compiled JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>

</head>
<body>

    <button class="back" onclick="window.location='../'">
      <img src="/src/icons/back.png">
    </button> <!-- Tilbake knapp -->

    <button class="ny_inventar" onclick="window.location='/admin/brukere/ny'">
      <img src="/src/icons/add.png">
    </button> <!-- Ny Bruker knapp -->

    <div class="container-fluid" id="utskjekkcontainer">
      <div class="row justify-content-center">
        <div class="col-mv-10 bg-light mt-5 rounded p-3">
          <h1 class="text-primary p-2">Brukere</h1>
          <hr>
          <div class="form-inline">
            <label for="search" class="font-weight-bold lead text-dark">Søk</label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="text" name="search" id="search_text" class="form-control form-control-lg rounded-0 border-primary" placeholder="Søk...">
          </div>
          <hr>
          <?php
            include "../../src/fn/init.php";
            $stmt=$conn->prepare("SELECT * FROM brukere");
            $stmt->execute();
            $result=$stmt->get_result();
          ?>
          <table class="table table-hover table-light table-striped" id="table_data">
            <thead>
              <tr>
                <th>#</th>
                <th>Fornavn</th>
                <th>Etternavn</th>
                <th>Utskjekket</th>
                <th>Admin</th>
                <th>Slett</th>
              </tr>
            </thead>
            <tbody>
              <?php while($row=$result->fetch_assoc()){ ?>
                <tr>
                  <td><?php echo $row["id"]; ?></td>
                  <td><?php echo $row["fornavn"]; ?></td>
                  <td><?php echo $row["etternavn"]; ?></td>
                  <td><?php echo $row["utskjekket"]; ?></td>
                  <td><?php echo $row["is_admin"]; ?></td>
                  <td><a href="delete.php?id=<?php echo urlencode($row['id']); ?>" onclick="return confirm('Er du sikker?');">Slett</a></td>
                </tr>
              <?php }
              $conn->close();
              ?>
             </tbody>

           </table>

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

    <script type="text/javascript">
      $(document).ready(function(){
        $('#search_text').keyup(function(){
          var search = $(this).val();
          $.ajax({
            url:'action.php',
            method:'post',
            data:{query:search},
            success:function(response){
              $('#table_data').html(response);
            }
          });
        });
      });
    </script>

</body>
</html>

动作.php:

<?php

    include "../../src/fn/init.php";
    $output = "";

    if(isset($_POST["query"])){
        $search = $_POST["query"];
        $stmt = $conn->prepare("SELECT * FROM brukere WHERE fornavn LIKE CONCAT('%',?,'%') OR etternavn LIKE CONCAT('%',?,'%') OR id LIKE CONCAT('%',?,'%')");
        $stmt->bind_param("sss",$search, $search, $search);
    } else {
        $stmt=$conn->prepare("SELECT * FROM brukere");
    };
    $stmt->execute();
    $result=$stmt->get_result();

    if($result->num_rows>0){
        $output = "<thead>
                    <tr>
                    <th>#</th>
                    <th>Fornavn</th>
                    <th>Etternavn</th>
                    <th>Utskjekket</th>
                    <th>Admin</th>
                    <th>Slett</th>
                    </tr>
                </thead>
                <tbody>";

        while($row=$result->fetch_assoc()){
            $output .= "
                <tr>
                    <td>".$row["id"]."</td>
                    <td>".$row["fornavn"]."</td>
                    <td>".$row["etternavn"]."</td>
                    <td>".$row["utskjekket"]."</td>
                    <td>".$row["is_admin"]."</td>
                    <td>".<a href="delete.php?id=<?php echo urlencode($row['id']); ?>" onclick="return confirm('Er du sikker?');">Slett</a>."</td>
                </tr>";
        };
        $output .= "</tbody>";
        echo $output;
    } else {
        echo "<h3>No match found!</h3>";
    };

?>

这是搜索脚本,问题是在while循环中添加了链接,我不知道如何使它工作。

piv4azn7

piv4azn71#

这个 a 标记不会作为纯文本添加到 output 字符串,您需要重写该行,将字符串部分正确地连接到变量部分。
尝试替换:

$output .= "
                <tr>
                    <td>".$row["id"]."</td>
                    <td>".$row["fornavn"]."</td>
                    <td>".$row["etternavn"]."</td>
                    <td>".$row["utskjekket"]."</td>
                    <td>".$row["is_admin"]."</td>
                    <td>".<a href="delete.php?id=<?php echo urlencode($row['id']); ?>" onclick="return confirm('Er du sikker?');">Slett</a>."</td>
                </tr>";

使用:

$output .= "
                <tr>
                    <td>".$row["id"]."</td>
                    <td>".$row["fornavn"]."</td>
                    <td>".$row["etternavn"]."</td>
                    <td>".$row["utskjekket"]."</td>
                    <td>".$row["is_admin"]."</td>
                    <td><a href='delete.php?id=".urlencode($row['id'])."' onclick='return confirm('Er du sikker?');'>Slett</a></td>
                </tr>";
wnrlj8wa

wnrlj8wa2#

要允许php从表结果回显id,只需执行php回显即可

<a href="delete.php?id=<? echo $row['id']; ?>" onclick="return confirm('Er du sikker?');">Slett</a>

您不必在这个阶段进行url编码,因为您只是简单地构建a:链接的href

fjaof16o

fjaof16o3#

你可以用它

<?php

include "../../src/fn/init.php";
$output = "";

if(isset($_POST["query"])){
    $search = $_POST["query"];
    $stmt = $conn->prepare("SELECT * FROM brukere WHERE fornavn LIKE CONCAT('%',?,'%') OR etternavn LIKE CONCAT('%',?,'%') OR id LIKE CONCAT('%',?,'%')");
    $stmt->bind_param("sss",$search, $search, $search);
} else {
    $stmt=$conn->prepare("SELECT * FROM brukere");
};
$stmt->execute();
$result=$stmt->get_result();

if($result->num_rows>0){
    $output = "<thead>
                <tr>
                <th>#</th>
                <th>Fornavn</th>
                <th>Etternavn</th>
                <th>Utskjekket</th>
                <th>Admin</th>
                <th>Slett</th>
                </tr>
            </thead>
            <tbody>";

    while($row=$result->fetch_assoc()){
        $output .= "
            <tr>
                <td>".$row["id"]."</td>
                <td>".$row["fornavn"]."</td>
                <td>".$row["etternavn"]."</td>
                <td>".$row["utskjekket"]."</td>
                <td>".$row["is_admin"]."</td>
                <td><a href=\"delete.php=".urlencode($row['id'])."\" onclick=\"return confirm('Er du sikker?');\">Slett</a> </td>  
            </tr>";
    };
    $output .= "</tbody>";
    echo $output;
} else {
    echo "<h3>No match found!</h3>";
};

相关问题