On document ready, I have the page calling ajax and displaying a table of results.
The rows can be updated by clicking a button, the button then calls a function which posts the update to the server.
I have this working without enclosing the click function in the document ready function but once I combine them it doesn't work.
html
<div id="newreqs"></div>
Javascript
$(document).ready(function(){
$('.approveCredit').on('click', function(e) {
e.preventDefault();
var creditid = $(this).attr('creditid');
var allocatedcreds = $(this).attr('allocatedcreds');
$.ajax({
url: "assets/ajax/addcustomer.php",
type: "POST",
data: {creditid: creditid,allocatedcreds: allocatedcreds},
success: function(result){
alert('Credit Applied!');
},
error: function(exception){
alert('Exception:' +exception);
}
});
});
$.post('assets/ajax/calldata/newreqs.php', function(data) {
$('#newreqs').html(data)
});
});
Data Called
<table class="table table-hover">
<thead>
<tr>
<th class="text-center" style="width: 50px;">#</th>
<th class="hidden-xs" style="width: 15%;">Name</th>
<th class="hidden-xs" style="width: 15%;">Requested Credits</th>
<th class="hidden-xs" style="width: 15%;">Notes from User</th>
<th class="hidden-xs" style="width: 15%;">PO</th>
<th class="hidden-xs" style="width: 15%;">Date Requested</th>
<th class="hidden-xs" style="width: 15%;">Status</th>
<th class="text-center" style="width: 15px;">Actions</th>
</tr>
</thead>
<tbody>
<?php
$count = 1;
while ($row = mysql_fetch_array($test))
{?>
<tr>
<td class="text-center"><?php echo $count++;?></td>
<td><?php echo $row['user_id'];?></td>
<td><?php echo $row['credits'];?></td>
<td><?php echo $row['notes'];?></td>
<td><?php echo $row['po'];?></td>
<td><?php echo $row['date_received'];?></td>
<td><?php echo $status;?></td>
<td class="text-center">
<div class="btn-group">
<a class="btn btn-xs btn-default approveCredit" type="button" allocatedcreds="<?php echo $row['credits'];?>" creditid="<?php echo $row['id'];?>" data-toggle="tooltip" data-original-title="Approve Credit Request"><i class="fa fa-check"></i></a>
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
As mentioned, this works when I don't encase the click function in the document ready function, however, when encasing nothing works.
The reason why I encased the click function is because I wanted to create a onpage refresh after the data had been updated on the server.