Millicent_ZA
Millicent_ZA

Reputation: 11

How can create a separate click event for each input button displayed using a while loop

I am listing users from the database in a table. for each user; there is an 'Approve' and 'Reject' button. I want to put my code in such way that when i click each button for each user,only the approval or the reject event should affect that specific user? My table looks like this:

enter image description here right now when i click 'approve' on one user, the other user's approval event also gets triggered. here is my approval code:

$sql = "select * from user_table ORDER BY date_registered DESC LIMIT 10";
if(mysqli_num_rows($result) > 0) 
 {
   while($row = mysqli_fetch_assoc($result)){
    $emailadr = $row["email"];
    $adresses [] = $emailadr;
    echo "<tr>";
    echo "<td>";
    echo $row["firstname"];
    echo "</td>";
    echo "<td>";
    echo $row["lastname"];
    echo "</td>";
    echo "<td>";
    echo $row["idnumber"];
    echo "</td>";
    echo "<td>";
    echo $row["qualification"];
    echo "</td>";
    echo "<td>";
    echo $row["dofa"];
    echo "</td>";
    echo "<td>";
    echo $row["date_registered"];
    echo "</td>";
    echo "<td>";
    echo '<button  name="approve" value='. $row["user_id"].'>Approve</button> <button  name="reject" value='. $row["user_id"].'>Reject</button>';
    echo "</td>";
    echo '</tr>';
     }
            if(isset($_POST['approve'])){
           // Approved user ID is now in $_POST['approve']
           mail($adresses[$row["user_id"]],'subect 
           1','approved','From:[email protected]');
       }elseif(isset($_POST['reject'])){
    // 
    }

  }

print_r($adresses);

Upvotes: 0

Views: 131

Answers (2)

Second2None
Second2None

Reputation: 1600

When inserting new users, you normally have a auto increment column that relates to each row or user (generally called ID) you can do it a few ways first thing you need to do is include that ID or value so you know which user to change:

Create your Buttons:

<button name="approve" value="5"> Approve</button>
<button name="reject" value="5"> Reject</button>

You would update the value to your user ID

You can then simply check whether you have Approved or Rejected the user:

if(isset($_POST['approve'])){
    // Approved user ID is now in $_POST['approve']
}elseif(isset($_POST['reject'])){
    // Rejected user ID is now in $_POST['reject']
}

To get $adresses you need to assign it outside the loop. Also updated $adresses [] to array_push($adresses, $row["email"]); just because of preference but can be changed back if you prefer.

<?php

$adresses = array();
$sql = "select * from user_table ORDER BY date_registered DESC LIMIT 10";
if(mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)){
        array_push($adresses, $row["email"]);
        echo "<tr>";
        echo "<td>";
        echo $row["firstname"];
        echo "</td>";
        echo "<td>";
        echo $row["lastname"];
        echo "</td>";
        echo "<td>";
        echo $row["idnumber"];
        echo "</td>";
        echo "<td>";
        echo $row["qualification"];
        echo "</td>";
        echo "<td>";
        echo $row["dofa"];
        echo "</td>";
        echo "<td>";
        echo $row["date_registered"];
        echo "</td>";
        echo "<td>";
        echo '<button name="approve" value='. $row["user_id"].'>Approve</button> <button name="reject" value='. $row["user_id"].'>Reject</button>';
        echo "</td>";
        echo '</tr>';
    }
    if(isset($_POST['approve'])){
        mail($adresses[$_POST["approve"]],'subect 1','approved','From:[email protected]'); 
        //update DB to approved
    }elseif(isset($_POST['reject'])){
        //update DB to rejected
    }

}

print_r($adresses);

?>

Upvotes: 2

deejay
deejay

Reputation: 15

Yes.. Passing the contentid or some unique identifier will solve this.

Upvotes: 0

Related Questions