I am implementing the following pagination example in ajax: How to display the page with ajax?
But every time I click on the following buttons or numbers on the page, it inserts the header and footer of the HTML page, that is, it is as if it inserted the whole page when receiving a response in ajax
$(function() {
$('#amount_show').change(function(evt) {
evt.preventDefault()
url = $(this).parent().attr('action')
ajaxLoad(url)
});
$('.data-table-pagination').on('click', '.pagination li a', function(evt) {
evt.preventDefault()
url = $(this).attr('data-target')
ajaxLoad(url)
});
function ajaxLoad(url) {
query_params = {
amount_show: $('#amount_show').val()
};
$('.data-table-pagination').html('<div class="loading">Loading...</div>')
$.ajax({
type: "GET",
url: url,
data: $.param(query_params),
success: function(data) {
$('.data-table-pagination').fadeOut('1000', function() { $(this).html(data) }).fadeIn('1000')
}
});
}
});
How can I avoid this error when clicking on the pagination buttons:
<a class="page-link" data-target="index.php?page=2">2</a>
Error in image capture:
Before clicking on the page:
After clicking on the page:
Following the recommendations I have separated the code:
index.php
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<div id="header">
<div class="header">
Hola
</div>
</div>
<div id="wrapper">
<div class="container">
<div class="data-pagination">
<div id="news-header" class="bootgrid-header container-fluid">
<div class="row">
<div class="col-sm-12 actionBar">
<div class="search-bar">
<!--<input type="text" id="myInput" placeholder="What are you looking for?">-->
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="What are you looking for?">
</div>
<div class="actions btn-group">
<select id="amount_show" name="amount_show">
<option value="10" <?php if ($records_by_page==10) echo "selected"; ?>>10</option>
<option value="25" <?php if ($records_by_page==25) echo "selected"; ?>>25</option>
<option value="50" <?php if ($records_by_page==50) echo "selected"; ?>>50</option>
<option value="100" <?php if ($records_by_page==100) echo "selected"; ?>>100</option>
</select>
</div>
</div>
</div>
</div>
<div class="data-table-pagination">
<?php echo $results_table; ?>
</div>
</div>
</div>
</div>
<!-- THE AJAX CODE IS HERE -->
<script type="text/javascript" src="assets/js/pag.min.js"></script>
pagination.php
<?php
$pagination_page = ''.$_SERVER['PHP_SELF'].'';
$defaul_records = 10;
if (isset($_GET['page'])) {
$page = $_GET['page'] ?: '';
}else{
$page = 1;
}
if (isset($_GET['amount_show'])) {
$records_by_page = $_GET['amount_show'];
} else {
$records_by_page = $defaul_records;
}
$localization_sql = ($page-1) * $records_by_page;
$sql = "SELECT id_product,image,product,price_old,price
FROM tbl_products
WHERE active=1
ORDER BY id_product ASC LIMIT $localization_sql, $records_by_page";
$stmt = $con->prepare($sql);
//$stmt->bind_param("i", $active);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows>0) :
// http://php.net/manual/es/function.ob-start.php
ob_start();
?>
<table id="myTable employee_table" class="table table-condensed table-hover table-striped bootgrid-table">
<thead>
<tr>
<th>ID</th>
<th>IMAGEN</th>
<th>PRODUCTO</th>
<th>PRECIO</th>
<th>ACCIÓN</th>
</tr>
</thead>
<tbody>
<?php
$stmt->bind_result($id_product,$image,$product,$price_old,$price);
while ($stmt->fetch()) {
echo '<tr>
<td>'.$id_product.'</td>
<td>';
echo'</td>
<td>'.$product.'</td>
<td>'.$price.'</td>
<td>
<span class="view_data" id="'.$id_product.'">Ver</span> |
<span class="edit_data" id="'.$id_product.'">Editar</span>
</td>
</tr>';
}
$stmt->close();
?>
</tbody>
</table>
<div class="pagination">
<ul class="pagination">
<?php
$sql = "SELECT * FROM tbl_products";
$stmt = $con->prepare($sql);
$stmt->execute();
$stmt->store_result();
$BD_records = $stmt->num_rows;
$stmt->close();
$con->close();
$total_page = ceil($BD_records / $records_by_page);
$prev = $page - 1;
$next = $page + 1;
if ($prev > 0) {
echo "<li><a data-target='".$pagination_page."?page=1'><i class='icon-angle-double-arrow'></i></a></li>";
echo "<li><a data-target='".$pagination_page."?page=$prev'><i class='icon-angle-left'></i></a></li>";
}
for ($i=1; $i<=$total_page; $i++) {
if ($page==$i) {
echo "<li><a class='page-link active' >". $page . "</a></li>";
} else {
echo "<li><a class='page-link' data-target='".$pagination_page."?page=$i'>$i</a></li>";
}
}
if ($page < $total_page ) {
echo "<li><a class='page-link' data-target='".$pagination_page."?page=$next'><i class='icon-angle-right'></i></a></li>";
echo "<li><a class='page-link' data-target='".$pagination_page."?page=$total_page'><i class='icon-angle-double-right'></i></a></li>";
}
$results_table = ob_get_clean();
else :
$stmt->close();
endif;
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
echo $results_table;
die;
}
?>
</ul>
</div>
I have followed the recommendations, but here I have a new problem, I can't understand how to link the separate code now.