Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an HTML table that gets its data dynamically from db table using PHP.
The idea is when user clicks Accept or Reject button, the label (pending) changes, but it's only the first row label change even if I clicked the button of the third row.

What I have tried:

I searched a lot and they said you must have a unique id for each row, and I tried to give each row a unique id (id="cert_num") but that is also not working, what did I miss?

This is the code:

PHP:

PHP
if ($result-> num_rows > 0){
  while ($row=$result-> fetch_assoc()) {  
   ?> 
 
    <tr id="<?=$row["cert_num"]?>">
    <td ><?=$row["Student_id"]?></td>
      <td><?=$row["cert_name"]?></td>
      <td><?=$row["cert_pro"]?></td>
      <td><?=$row["cert_date"]?></td>      
      <td><?=$row["cert_file"]?></td>
      <td><label name="pending" id="pending">Pending</label></td>
      <td><button name="accept" id="accept" onclick="StatusAccept()">Accept </button></td>
      <td> <button name="reject" id="reject" onclick="StatusReject()">Reject</button></td>

    </tr> 

      <?php }} ?>


JS:
JavaScript
var pending = document.getElementById("pending");

    function StatusAccept() {
         
        pending.innerHTML='<label style="background: green"> 
        Accepted</label>'
    }

    function StatusReject() {
        pending.innerHTML='<label style="background: red"> 
        Rejected</label>'
}
Posted
Updated 25-Oct-23 7:12am
v3

1 solution

IDs in an HTML document need to be unique. You are generating an HTML document with duplicate IDs - for example, every row has a button with id="pending". When you ask the document for the element with the ID "pending", it will only return a single element. In this case, it returns the first one it finds, but there is no guarantee that it will always pick a specific one.

You need to change your code to use a unique ID for each element. Alternatively, find a different way to identify the elements you want to work with. For example, using Event Delegation[^] :
HTML
<td><label name="pending">Pending</label></td>
<td><button name="accept">Accept </button></td>
<td> <button name="reject">Reject</button></td>
JavaScript
document.getElementById("YourTableId").addEventListener("click", function(e) {
    let target = e.target;
    if (target.tagName !== "BUTTON") { target = target.closest("button"); }
    if (!target) { return; }
    
    let statusText, statusColor;
    if (target.name === "accept") {
        statusText = "Accepted";
        statusColor = "green";
    } else if (target.name === "reject") {
        statusText = "Rejected";
        statusColor = "red";
    } else {
        console.warn("Unknown button:", target);
        return;
    }
    
    const pending = target.closest("tr").querySelector("label[name='pending']");
    pending.innerHTML = statusText;
    pending.style.backgroundColor = statusColor;
});
 
Share this answer
 
Comments
Member 16116918 17-Oct-23 17:26pm    
yess I understand it now and it works! Thanks so much for helping <3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900