title: How to use Modal in FOREACH
body: <p>I have a modal which pops up when selecting a button. This modal should populate the user's name.</p>
  
  <p>The problem is that, when I select the button, it only populates the first user's details and not the rest of the users in the table. I dont understand why. If anyone can point out why I would be much obliged.</p>
  
  <p>Here is my code </p>
  
  <pre><code>&lt;?php
      class afficherUser {
          function connection(){
              $con=mysql_connect('localhost','root','');
              mysql_select_db('UserTable');
          }
  
          function ShowUser(){
              $sql="SELECT*FROM users  LIMIT 3 ";
              $req= mysql_query($sql);
  
              while ($data=mysql_fetch_assoc($req)) {
                  $tab[]=$data;
              }
              return $tab;
          }    
      }
  
      $data=new afficherUser();
      $data-&gt;connection();
      $result=$data-&gt;ShowUser();
  
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;link rel="stylesheet" href="style.css"&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;?php foreach ($result as $value):?&gt;
  
  &lt;!-- Trigger/Open The Model --&gt;
  &lt;button id="myBtn" class="myBtn"&gt;Open Modal&lt;/button&gt;
  
  &lt;!-- The Modal --&gt;
  &lt;div id="myModal" class="modal"&gt;
  
    &lt;!-- Modal content --&gt;
    &lt;div class="modal-content"&gt;
      &lt;div class="modal-header"&gt;
        &lt;span class="close"&gt;×&lt;/span&gt;
        &lt;h2&gt;Modal Header&lt;/h2&gt;
      &lt;/div&gt;
      &lt;div class="modal-body"&gt;
  
     &lt;?php echo $value["user"]." ".$value["u_id"];?&gt;
  
  
      &lt;/div&gt;
      &lt;div class="modal-footer"&gt;
        &lt;h3&gt;Modal Footer&lt;/h3&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
  &lt;/div&gt;
  
  
  &lt;?php endforeach; ?&gt;
  
  &lt;script&gt;
  // Get the modal
  var modal = document.getElementById('myModal');
  
  // Get the button that opens the modal
  var btn = document.getElementById("myBtn");
  
  // Get the &lt;span&gt; element that closes the modal
  var span = document.getElementsByClassName("close")[0];
  
  // When the user clicks the button, open the modal 
  btn.onclick = function() {
      modal.style.display = "block";
  }
  
  // When the user clicks on &lt;span&gt; (x), close the modal
  span.onclick = function() {
      modal.style.display = "none";
  }
  
  // When the user clicks anywhere outside of the modal, close it
  window.onclick = function(event) {
      if (event.target == modal) {
          modal.style.display = "none";
      }
  }
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>