将值传递到表行上的下一页单击?

时间:2018-03-22 05:03:22

标签: php html

我有一张目前无法点击的表格。在行单击时,我想从此表转到详细信息页面,在过程中传递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值传递给详细信息页面?

4 个答案:

答案 0 :(得分:2)

您需要制作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 }

答案 1 :(得分:0)

MyFunction2

答案 2 :(得分:0)

<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()函数设置为您的标记。试试这个。

答案 3 :(得分:0)

您需要更改while循环,如下所示:在点击

上添加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>
        ";
}?>