title: Passing loop data as variable from JavaScript to AJAX via Bootstrap modal
body: <p>My code below was used to retrieve records from a loop using  JavaScript. Everything works fine. Each of the records button has a unique id that when click 
  should alert <code>users_id</code> in a Bootstrap Modal popup.</p>
  
  <p>The problem is that the looped button that contains <code>users_id</code> is not alerting anything  when each button is clicked.</p>
  
  <p>Below is the code for retrieving records in JavaScript along with Bootstrap modal button:</p>
  
  <pre><code>&lt;script&gt;
      $(document).ready(function () {
          $.post('users.php', function(response){
              $.each(JSON.parse(response).items, function(i,v) {
                  $('.userslist').append('&lt;span&gt;'+v.id+'&lt;/span&gt; &lt;a href="#myModal" id="'+v.id+'" class="modalLink"  data-toggle="modal" data-target="#myModal"&gt;Profile&lt;/a&gt;');
              });
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>Below is the code  for posting the <code>users_id</code> and then alert it:</p>
  
  <pre><code>&lt;script&gt;
      $(document).ready(function(){
          $(".modalLink").click(function() {
              var id = $(this).attr("id");
              alert(id);
              var dataString = 'id='+ id;
              $.ajax({
                  type: "POST",
                  url: "ajax_modal.php",
                  data: dataString,
                  cache: false,
                  success: function(data) {      
                      $("#rmm").html(data);
                  } 
              });
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>but if I take the Modal button outside the retrieve records in a javascript loop
  it will alert users_id and everything will work fine as in code below</p>
  
  <pre><code>&lt;a href="#myModal" id="'+v.id+'" class="modalLink"  data-toggle="modal" data-target="#myModal"&gt;Profile&lt;/a&gt;
  </code></pre>
  
  <p>Can someone help me make each of the JavaScript  looped button to post its own unique <code>users_id</code> and then alert it in a Bootstrap modal popup. I have attached a screen shot of the result obtained from the JavaScript loop.</p>
  
  <p><a href="https://i.stack.imgur.com/Yf5qI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Yf5qI.jpg" alt="Example Result"></a></p>
  
  <p>Thanks</p>