Using HTML buttons in PHP loops

I have a html table displayed using foreach loop in php. And I do even have buttons to be clicked in multiple rows. And the code goes like this:

<form method="post">
<table>
<tr> <th> Item </th> <th>Click to select</th></tr>
<?php 
     $query="select items from items_table";
     $result=$con->query($query);         //$con is connection variable already initialized
     $row=mysqli_fetch_assoc($result);
     foreach ($row as $index)           //loop
     {
?>
        <tr>
            <td><?php echo $index['items']; ?>  </td>
            <td><input type="button" value="select"> </td>     //button here
        </tr>
<?php } ?>

</table>
</form>
             

Now how can I get to know which button was pressed? I have read some web pages, which says we need to be using AJAX, and I'm a newbie with no knowledge of how it works.. Please help me out!

I tried to have button inside a loop and expected that the buttons works correctly directly. But it gives wrong output.

Answer

Solution:

If I got what u want I'd say you should handle your button or input Some ways available

 $index['items'] 

Is Not Correct where u used

  1. <input value=<?php echo $row['id'] ?>
    
  2. <input name=foo[<?php echo $row['id'] ?>] value=<?php echo $row['id'] ?> >
    
  3. <input name='foo[]' value=<?php echo $row['id'] ?> >
    

Then handle them :

 <?php
  foreach($_REQUEST['foo'] as $name => 
  $value){
   echo $name ."posted and its value 
   is:". $value;
  }
 ?>

OR

 <?php
  echo 'value of foo[1] Is '.$_REQUEST['foo'][1] ;
 ?>

You can use FOR EXAMPLE $row['name'] Or any field name u have in your table intead of $row['id'] that I gave

Source