Add onclick event on a php-generated table cell

Mantas

I would like to generate an HTML table with dynamic content in PHP, and add an onclick event on a cell of each row, to trigger an action depending on that row.

How to achieve that ?

Here is my example : I have a table which prints data from a database. Table is made of 3 columns. Columns 1 and 2 hold name and surname and column 3 refers to a comment. Third column says "Comment exists" or "No comments", depending on whether a related comment exists or not.

The goal is to pop up a small window when user clicks on the third column, to show the given comment.

Here is an example of a table, and what I have tried so far :

<table border=1>
   <tr>
      <th>ID</th>
      <th>Vardas</th>
      <th></th>
   </tr>
<?php while($row = mysql_fetch_array($res)) : ?>
   <tr>
      <td><?php echo $row['id'] ?></td>
      <td><?php echo $row['vardas'] ?></td>
      <td><?php echo $row['gedimas'] ?></td>
   </tr>
<?php endwhile; ?>
</table>

How do I add on click event for the $row['gedimas'] line, to display a full comment from database ?

Vic Seedoubleyew

You can place an "onclick" event directly in PHP :

<td <?php echo "onclick='displayComment(".$id.")'" ?> >

About the general structure of opening a window, you can build the table pretty easily, and then have another page for displaying the comment if there is.

Code for the table :

<table>
  <tr>
    <th>Name</th>
    <th>Surname</th>
    <th>Has comment ?</th>
  </tr>
<?php
for( $i = 0; $i<count($database_data); $i++){
   $item = $database_data[$i];
   ?><tr>
       <td><?php echo $item[0] ?></td>
       <td><?php echo $item[1] ?></td>
       <td <?php 
          echo "onclick='displayComment("
          echo $item[0].", ".$item[1]
          echo ")'"?>
       ><?php echo $item[2] ? "Comment exists" : "No comments" ?></td>
     </tr>
}

Then you would need to include a javascript file with the following code :

function displayComment(name, surname) {
   window.open(
      "display_comment.php?name="+encodeURI(name)+"&surname="+encodeURI(surname),
      "_blank",
      "height=200, width=300"
}

And lastly, you would need your display_comment.phppage, whose essence would be :

// get comment from database using data in $_GET["name"] and $_GET["surname"]

Comment from <?php echo $_GET["name"]." ".$_GET["surname"] ?> : 
<br>
<?php echo $comment_retrieved_from_database ?>


Alternative

An alternative would be to only record the id of the comment in the HTML code, and then add the onclick events in javascript.

The php code for the cell would be as follows :

<td <?php echo "data-comment-id='$id'" ?> >

And you would add the onclick events in javascript that way :

var tableCells = document.querySelectorAll("td[data-comment-id]");
for(var i=0; i<tableCells.length; i++){
   var cell = tableCells[i];
   var commentId = cell.getAttribute("data-comment-id");
   cell.addEventListener("click", function(){openPopup(commentId);});
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to add click event to table cell in this code?

From Dev

How to add click event to table cell in this code?

From Dev

How to add an Id, value and onclick event to a dynamic generated div

From Dev

php table add values if cell repeats

From Dev

Dynamically generated div onclick event

From Dev

dynamically add table row having buttons with onclick event

From Dev

Add an onClick event to append()

From Dev

React onClick add an X to the cell

From Dev

OnClick event for buttons dynamically generated from array

From Dev

Add multiple MySQL results to one cell in a HTML table using PHP

From Dev

Add onclick to element, but keep current onclick event

From Dev

Add dynamic variable to onclick event

From Dev

Add onclick event via JavaScript

From Dev

Add dynamic variable to onclick event

From Dev

JavaScript onClick event - HTML table

From Dev

add onclick event on table created by js using append and navigate to show relevant data

From Dev

AngularJS: Change cell class in a dynamically generated table based on cell value

From Dev

jQuery add dynamically cell to a static cell into table

From Dev

Add overflow table cell in a cell with a header

From Dev

Handling LinkButton event of dynamicaly generated table?

From Dev

Click event not working on button of dynamic generated table

From Dev

jQuery onClick only runs once on table cell

From Dev

onClick HTML table cell save into django database

From Dev

Creating an auto generated table with php

From Dev

Ternary operator in PHP generated table

From Dev

Styling an html table generated with php

From Dev

Auto add empty table cell

From Dev

Add Cell in the Table View Controller

From Dev

How to add to html <a> tag onclick event with PHP and call the function in JavaScript file

Related Related

  1. 1

    How to add click event to table cell in this code?

  2. 2

    How to add click event to table cell in this code?

  3. 3

    How to add an Id, value and onclick event to a dynamic generated div

  4. 4

    php table add values if cell repeats

  5. 5

    Dynamically generated div onclick event

  6. 6

    dynamically add table row having buttons with onclick event

  7. 7

    Add an onClick event to append()

  8. 8

    React onClick add an X to the cell

  9. 9

    OnClick event for buttons dynamically generated from array

  10. 10

    Add multiple MySQL results to one cell in a HTML table using PHP

  11. 11

    Add onclick to element, but keep current onclick event

  12. 12

    Add dynamic variable to onclick event

  13. 13

    Add onclick event via JavaScript

  14. 14

    Add dynamic variable to onclick event

  15. 15

    JavaScript onClick event - HTML table

  16. 16

    add onclick event on table created by js using append and navigate to show relevant data

  17. 17

    AngularJS: Change cell class in a dynamically generated table based on cell value

  18. 18

    jQuery add dynamically cell to a static cell into table

  19. 19

    Add overflow table cell in a cell with a header

  20. 20

    Handling LinkButton event of dynamicaly generated table?

  21. 21

    Click event not working on button of dynamic generated table

  22. 22

    jQuery onClick only runs once on table cell

  23. 23

    onClick HTML table cell save into django database

  24. 24

    Creating an auto generated table with php

  25. 25

    Ternary operator in PHP generated table

  26. 26

    Styling an html table generated with php

  27. 27

    Auto add empty table cell

  28. 28

    Add Cell in the Table View Controller

  29. 29

    How to add to html <a> tag onclick event with PHP and call the function in JavaScript file

HotTag

Archive