I have a jquery code that fetched the name of the users saved in mysql.
<script>
var currentID = null;
function fetch_data() {
$.ajax({
url: "select.php",
method: "POST",
success: function(data) {
$('#live_data').html(data);
//fetch_chat();
}
});
}
$(document).ready(function() {
fetch_data();
$(document).on('click', '.first_name', function() {
currentID = $(this).data("id1");
alert(currentID);
});
});
</script>
and the select.php looks like this:
<?php
session_start();
require("config.php");
$output = '';
$sql = "SELECT * FROM users where name!='$_SESSION[name]'";
$result = mysqli_query($con, $sql);
$output .= '
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="40%">First Name</th>
</tr>';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td class="first_name" data-id1="'.$row['id'].'">'.$row["name"].'</td>
</tr>
';
}
}
else
{
$output .= '<tr>
<td colspan="4">Data not Found</td>
</tr>';
}
$output .= '</table>
</div>';
echo $output;
?>
The problem is that I am not geeting the id of the clicked user. the code is to fetch the data from the table and when clicked on any name it should alert their respective id.