Because you are assigning the same ID to each <td> (which is invalid HTML), so you are getting the value for the first element found with that ID.
<table border="1">
<?php for ($i=1; $i<=5; $i++) {
echo "<tr id='tr_$i' onclick=\"alert(document.getElementById('td_$i').innerHTML)\">\n";
echo "<td id='td_$i'>$i</td>\n";
echo "</tr>\n";
} ?>
</table>