javascript - How to create a clickable html table with php?

245

I just tried creating a clickable table to show more information by clicking on a table row.

This is my code for the table creation:

while ($data= mysqli_fetch_assoc($query))
{
    echo "<tr>";
    echo "<td style='text-align: center;'>" . $data['number'] . "</td>";
    echo "<td>" . $data['name'] . "</td>";
    echo "</tr>";
}

How can I create a click event on every specific table row which calls a function with the unique id of the entry?

285

Answer

Solution:

There's lots of ways you can go about doing this. The simplest way here would be to add an onclick listener to the row

while ($data= mysqli_fetch_assoc($query))
    {
        echo "<tr onclick=myfunction() id=". $data['unique_identifier'].">";
        echo "<td style='text-align: center;'>" . $data['number'] . "</td>";
        echo "<td>" . $datensatz['name'] . "</td>";
        echo "</tr>";
    }

And myfunction sends an ajax request or you preload the data and just display the data. The id serves as either mysql where clause or as a getter in case the data is preloaded

918

Answer

Solution:

while ($data= mysqli_fetch_assoc($query))
    {
    echo "<tr>";
    echo "<td style='text-align: center;'>" . $data['number'] . "</td>";        echo "<td>" . $datensatz['name'] . "</td>"; ?>
    <td> 
       <a href="yourprog.php?number=<? echo $data['number'] ?>&name=<? echo $data['name'] ?>" ></a>
    </td><?
    echo "</tr>";
    }

People are also looking for solutions to the problem: How to Pass A Session Variable in Zend To Native PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.