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 users_id
in a Bootstrap Modal popup.
The problem is that the looped button that contains users_id
is not alerting anything when each button is clicked.
Below is the code for retrieving records in JavaScript along with Bootstrap modal button:
<script>
$(document).ready(function () {
$.post('users.php', function(response){
$.each(JSON.parse(response).items, function(i,v) {
$('.userslist').append('<span>'+v.id+'</span> <a href="#myModal" id="'+v.id+'" class="modalLink" data-toggle="modal" data-target="#myModal">Profile</a>');
});
});
});
</script>
Below is the code for posting the users_id
and then alert it:
<script>
$(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);
}
});
});
});
</script>
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
<a href="#myModal" id="'+v.id+'" class="modalLink" data-toggle="modal" data-target="#myModal">Profile</a>
Can someone help me make each of the JavaScript looped button to post its own unique users_id
and then alert it in a Bootstrap modal popup. I have attached a screen shot of the result obtained from the JavaScript loop.
Thanks