Sang Tonsing
Sang Tonsing

Reputation: 147

Identifying the button clicked inside while loop in php?

I am creating how a normal messaging works and I want to put the msg number depending on the number of new msg(1,2,...), and to put that, instead of using a link I used a button so that after clicking the button there won't be any new msg anymore(which means data is inserted in database) but I have a problem identifying that particular button which I clicked, after which it should go to the conversation with that particular person. Here's my sample code(without the SQL).

foreach($chat_list as $friend) {
    echo "<form action = '' method = 'post'>";
    echo "<button id = 'chats'>".$friend."</button><br>";
    echo "</form>";
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        echo "<script> location.replace('chat.php?usernames=".$friend."'); </script>";
    }
}

The problem here is that, for example, there are 3 persons that I chat to(meaning 3 buttons), whenever I clicked the 2nd or 3rd person it redirects to the chat.php page but in the 1st person conversation which means that all three buttons conversation is with the 1st person. What I want is when I clicked the 2nd person it should redirect to the 2nd person conversation, not with 1st, and same goes to 3rd person. I hope I am clear. Thank You for your help

Upvotes: 0

Views: 341

Answers (1)

LeonidMew
LeonidMew

Reputation: 502

To identify which button pressed in POST request in php - buttons should have different name attribute. Then you`ll have $_POST['buttonname'] = 'text of the button'
The id attribute only visible by javascript, not on server side.

Upvotes: 1

Related Questions