【问题标题】:Pass value to next page on table row click?将值传递到表格行单击的下一页?
【发布时间】:2018-03-22 13:40:00
【问题描述】:

我有一个当前不可点击的表格。单击行时,我想从该表转到详细信息页面,并在过程中传递 id 值。

我知道我需要在表格行上创建一个 href 并以某种方式在点击时传递 id 值,但不清楚如何执行此操作。相关表和php:

  <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
                                <div class="table-responsive m-t-40">
                                    <table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
                                        <thead>
                                            <tr>
                                            <thead><tr><th title="Field #1">id</th>
                                            <th title="Field #2">Organization</th>
                                            <th title="Field #3">xxx</th>
                                            <th title="Field #4">xxx</th>
                                            <th title="Field #5">xxx</th>
                                            <th title="Field #6">xxx</th>
                                            </tr>
                                        </thead>
                                        <tbody>

                                            <?php

                                                $servername = "xxx";
                                                $username = "xxx";
                                                $password = "xxx";
                                                $dbname = "xxx";

                                                // Create connection
                                                $conn = new mysqli($servername, $username, $password, $dbname);

                                                // Check connection
                                                if ($conn->connect_error) {

                                                    die("Connection failed: " . $conn->connect_error);

                                                }else{

                                                    echo '<script>console.log("Connection successful!")</script>';

                                                }

                                                $SELECT = mysqli_query($conn,"SELECT * FROM `organization`");

                                                if($SELECT != false)
                                                {
                                                    while($rows = mysqli_fetch_array($SELECT))
                                                    {
                                                        echo "
                                                            <tr>
                                                                <td>".$rows["id"]."</td>
                                                                <td>".$rows["name"]."</td>
                                                                <td>".$rows["xxx"]."</td>
                                                                <td>".$rows["xxx"]."</td>
                                                                <td>".$rows["xxx"]."</td>
                                                                <td>".$rows["xxx"]."</td>
                                                            </tr>
                                                            ";
                                                    }
                                                }else{
                                                    echo "
                                                        <tr>
                                                        <td colspan='3'>Something went wrong with the query</td>
                                                        </tr>
                                                    ";
                                                }

                                            ?>

                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

如何使我的表格行可点击(以转到详细信息页面)以及如何将点击时表格中的 id 值传递到详细信息页面?

【问题讨论】:

  • 在下面查看我的答案:它将适用于整个

标签: php html


【解决方案1】:

您需要制作href 标签并像这样传递id

<a href="paganame?id=<?php echo $id; ?>">click</a>

在你的情况下应该是

<td><a href="detail.php?id=<?php echo $rows["id"]; ?>">click</a></td>

你的while循环应该是这样的

while($rows = mysqli_fetch_array($SELECT)){ ?>
   <tr>
        <td><a href="paganame?id=<?php echo $rows["id"]; ?>">click</a></td>
        <td><?php echo $rows["name"]; ?></td>
        <td><?php echo $rows["xxx"]; ?></td>
        <td><?php echo $rows["xxx"]; ?></td>
        <td><?php echo $rows["xxx"]; ?></td>
        <td><?php echo $rows["xxx"]; ?></td>                                                                   <td>".$rows["xxx"]."</td>
    </tr>                                               
 <?php }

【讨论】:

  • 我把它放在哪里?它在 php 代码中的 中吗?我必须转义任何字符吗?
  • 导致 HTTP 错误 500
  • 当我使用该代码时,标签似乎没有正确关闭
  • @arcade16 很高兴为您提供帮助
  • 删除点击粘贴
【解决方案2】:
<td><a href="nextpage.php?id=<?php echo $id; ?>">".$rows["xxx"]."</a></td>

【讨论】:

    【解决方案3】:
    <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body">
                            <div class="table-responsive m-t-40">
                                <table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
                                    <thead>
                                       <tr><th title="Field #1">id</th>
                                        <th title="Field #2">Organization</th>
                                        <th title="Field #3">xxx</th>
                                        <th title="Field #4">xxx</th>
                                        <th title="Field #5">xxx</th>
                                        <th title="Field #6">xxx</th>
                                        </tr>
                                    </thead>
                                    <tbody>
    
                                        <?php
    
                                            $servername = "xxx";
                                            $username = "xxx";
                                            $password = "xxx";
                                            $dbname = "xxx";
    
                                            // Create connection
                                            $conn = new mysqli($servername, $username, $password, $dbname);
    
                                            // Check connection
                                            if ($conn->connect_error) {
    
                                                die("Connection failed: " . $conn->connect_error);
    
                                            }else{
    
                                                echo '<script>console.log("Connection successful!")</script>';
    
                                            }
    
                                            $SELECT = mysqli_query($conn,"SELECT * FROM `organization`");
    
                                            if($SELECT != false)
                                            {
                                                while($rows = mysqli_fetch_array($SELECT))
                                                {
                                                    echo "
                                                        <tr  onclick="window.location='detail.php?id=$rows["id"]';">
                                                            <td>".$rows["id"]."</td>
                                                            <td>".$rows["name"]."</td>
                                                            <td>".$rows["xxx"]."</td>
                                                            <td>".$rows["xxx"]."</td>
                                                            <td>".$rows["xxx"]."</td>
                                                            <td>".$rows["xxx"]."</td>
                                                        </tr>
                                                        ";
                                                }
                                            }else{
                                                echo "
                                                    <tr>
                                                    <td colspan='3'>Something went wrong with the query</td>
                                                    </tr>
                                                ";
                                            }
    
                                        ?>
    
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
    

    您可以使用纯 JavaScript 为您的标签设置 onclick() 函数。试试这个。

    【讨论】:

    • 这也会导致 HTTP 错误 500
    • 有 2 个 标签.. 你不需要在 1.. 内写另一个 thead .. 编辑的代码请检查它..
    • 仍然导致 HTTP 错误 500
    【解决方案4】:

    你需要改变你的while循环如下:在tr点击上添加window.location

    <?php
    while($rows = mysqli_fetch_array($SELECT))
    {
    
        echo "
            <tr  onclick=\"window.location='detail.php?id=".$rows["id"]."'\">
                <td>".$rows["id"]."</td>
                <td>".$rows["name"]."</td>
                <td>".$rows["xxx"]."</td>
                <td>".$rows["xxx"]."</td>
                <td>".$rows["xxx"]."</td>
                <td>".$rows["xxx"]."</td>
            </tr>
            ";
    }?>
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签