I'm trying to get first cell (td
) for each row and getting it but only for current page. If I navigate to next page then the checkbox checked on the previous page is not being sent.
<table class="table" id="example2">
<thead><tr>
<th>Roll no</th><th>Name</th></tr><thead>
<?php
$sel = "SELECT * FROM `st`";
$r = mysqli_query($dbc, $sel);
while ($fet = mysqli_fetch_array($r)) {
?>
<tr>
<td><?php echo $fet['trk'] ?></td>
<td><input type="text" value="<?php echo $fet['ma'] ?>" id="man" class="form-control"></td>
<td><input type="checkbox" id="check" name="myCheckbox" class="theClass"></td></tr>
<?php } ?>
</table>
<input type="submit" id="sub_marks" class="btn btn-info" value="Submit & Continue">
<script src="plugins/datatables/jquery.dataTables.min.js" type="text/javascript"></script>
<script src="plugins/datatables/dataTables.bootstrap.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('#example2').DataTable({
"paging": true,
"lengthChange": false,
"searching": false,
"ordering": true,
"info": true,
"autoWidth": false,
})
});
</script>
<script>
$('#sub_marks').click(function () {
var values = $("table #check:checked").map(function () {
return $(this).closest("tr").find("td:first").text();
}).get();
alert(values);
})
</script>
and add script with correct form id and table id
This is working code
CAUSE
jQuery DataTables removes non-visible rows from DOM for performance reasons. When form is submitted, only data for visible checkboxes is sent to the server.
SOLUTION 1. Submit form
You need to turn elements
<input type="checkbox">
that are checked and don't exist in DOM into<input type="hidden">
upon form submission.SOLUTION 2: Send data via Ajax
DEMO
See jQuery DataTables: How to submit all pages form data for more details and demonstration.
NOTES
value
attribute assigned with unique value.id
attributecheck
for multiple elements, this attribute is supposed to be unique.paging
,info
, etc. options for jQuery DataTables, these are enabled by default.htmlspecialchars()
function to properly encode HTML entities. For example,<?php echo htmlspecialchars($fet['trk']); ?>
.