arcade16
arcade16

Reputation: 1535

Pass value to next page on table row click?

I have a table that is not currently clickable. On row click, I would like to go to a detail page from this table, passing the id value in the process.

I understand I need to make an href on the table row and somehow pass the id value on click, but am not clear on how to do this. Relevant table and 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>

How do I make my table rows clickable (to go to the detail page) and how do I pass the id value from the table on click to the detail page?

Upvotes: 1

Views: 1768

Answers (4)

Rahul
Rahul

Reputation: 1615

you need to make href tag and pass the id like this

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

in your case it should be

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

your while loop should be like this

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 }

Upvotes: 2

B. Desai
B. Desai

Reputation: 16436

You need to change your while loop as below: add window.location on tr click

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

Upvotes: 0

Saranya Rajendran
Saranya Rajendran

Reputation: 757

<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>

You can set onclick() function to your tag using plain javascript.. try this.

Upvotes: 0

Rp9
Rp9

Reputation: 1963

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

Upvotes: 0

Related Questions