I am displaying some records in an HTML table using PHP. I am trying to make it so that when I click on a row, I can store the itemID (that has been clicked on) in a variable. For some reason, when I click on the row I want, the alert in JavaScript just doesn't work nor does it happen. Am I missing something trivial?
<?php
$con=mysqli_connect("localhost","root","mypassword","myDB");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "<font color = 'darkgreen'> Connected to database. </font> <br>";
$result = mysqli_query($con,"SELECT * FROM Inventory");
echo "<table border='1'>
<tr>
<th>Item ID</th>
<th>Item Name</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['itemID'] . "</td>";
echo "<td>" . $row['itemName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<script type="text/javascript">
$("tr.table").click(function() {
var tableData = $(this).children("td").map(function() {
return $(this).text();
}).get();
alert("Your data is: " + $.trim(tableData[0]) + " , " + $.trim(tableData[1]));
});
</script>
echo json_encode($assocArray). Oh, the font tag is dead. Just advice. The comments by@Alberto Rubioare what you need to know.