I have this simple javascript function that makes a POST request and grabs the reply, then sets the HTML of an element to the reply. Here is the function:
function updateStatus(vmid) {
action = 'vmstatuslabel';
label = $('#status-'+vmid);
$.post(site + '/application/controllers/ajax.php', {action:action, vmid:vmid}, function(data) {
console.log(data);
label.html(data);
});
}
This works fine when doing something like updateStatus(13), but running it multiple times will ONLY update the last element in the table. Here is the table in which the script tag is echoed:
<?php
if($uvms = $vmhandler->userVMs()) {
# User has some VMs
?>
<table class="table table-condensed table-striped">
<?php
foreach($uvms as $vm) {
?>
<tr>
<td><a href="<?=$site;?>/manage?vid=<?=$vm['id'];?>"><?=$vm['name'];?></a></td>
<td><?=$vmhandler->renderControls($vm['id']);?></td>
<td id="status-<?=$vm['id'];?>">Test</td>
<td><?=$vm['ipaddr'];?></td>
</tr>
<?php
$vmid = $vm['id'];
echo "<script>updateStatus($vmid)</script>";
}
?>
</table>
The last element in the table has its status colum updated to whatever the updateStatus function returns, however the rest of the elements remain as "Test."
Even something like this does not work:
function updateStatus(vmid) {
action = 'vmstatuslabel';
label = $('#status-'+vmid);
$.post(site + '/application/controllers/ajax.php', {action:action, vmid:vmid}, function(data) {
console.log(data);
label.html("ABCD");
});
}
Only the last element in the table is set to ABCD.
This, however, does work, and updates every value to ABCD:
function updateStatus(vmid) {
label = $('#status-'+vmid);
label.html("ABCD");
}