我不会问,但我真的需要。
好吧,我正在尝试用ajax更新我的DataTable。我的一张桌子还可以,但另一张不是。
这是代码。 第一个是用户将访问的PHP。第二个是AJAX网址。
p.s:我的阵列名称是葡萄牙语,对不起。
clientes.php
<?php
// Aqui será chamado o BD e conexões necessárias.
require_once("db.php");
try {
$sql = "SELECT * FROM telesena.clientes;";
$resultado = $con->query($sql);
} catch (PDOException $e){
echo "Estamos passando por um problema. Tente novamente mais tarde.";
}
header( 'Content-Type: text/html; charset=utf-8' );
?>
<html>
<meta charset="utf-8">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs/jszip-2.5.0/pdfmake-0.1.18/dt-1.10.13/b-1.2.4/b-html5-1.2.4/sc-1.4.2/datatables.min.css"/>
<!-- Scripts necessários para DataTable e jQuery -->
<title> Pedidos Tele-Sena </title>
<!-- Chamando o Bootstrap -->
<link rel="stylesheet" href="css/bootstrap.css">
<body>
<!-- Nav principal -->
<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
<div class="collapse navbar-collapse" id="navbarNav">
<a class="navbar-brand"> Logo da Empresa </a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="index.php">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="pedidos.php">Pedidos</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Clientes</a>
</li>
</ul>
</div>
</nav>
<!-- Começando a fazer a tabela de clientes -->
<div class="container">
<table id="clientes" class="table" cellspacing="0" width="100%">
<thead>
<tr>
<th>Cód. Convênio</th>
<th>Nome Fantasia</th>
<th>Razão Social</th>
<th>Logradouro</th>
<th>Número</th>
<th>Bairro</th>
<th>Município</th>
<th>Estado</th>
<th>UF</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/bs/jszip-2.5.0/pdfmake-0.1.18/dt-1.10.13/b-1.2.4/b-html5-1.2.4/sc-1.4.2/datatables.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.toaster.js"></script>
</body>
<script>
$(document).ready(function(){
$('#clientes').DataTable({
"serverSide":true,
"oProcessing":true,
"ajax":"ajaxclientes.php",
"processing":true,
"bSort":false,
"bPaginate":false,
"language":{
"sEmptyTable": "Nenhum registro encontrado",
"sInfo": "Mostrando de _START_ até _END_ de _TOTAL_ registros",
"sInfoEmpty": "Mostrando 0 até 0 de 0 registros",
"sInfoFiltered": "",
"sInfoPostFix": "",
"sInfoThousands": ".",
"sLengthMenu": "Clientes por página: _MENU_ ",
"sLoadingRecords": "Carregando...",
"sProcessing": "Processando...",
"sZeroRecords": "Nenhum registro encontrado",
"sSearch": "Pesquisar ",
"oPaginate": {
"sNext": "Próximo",
"sPrevious": "Anterior",
"sFirst": "Primeiro",
"sLast": "Último"
},
"oAria": {
"sSortAscending": ": Ordenar colunas de forma crescente",
"sSortDescending": ": Ordenar colunas de forma descendente"
}
}
});
});
</script>
<!-- Modal padrão e todos os scripts necessários estão sendo chamados do info.php-->
</html>
ajaxclientes.php
<?php
header("Content-Type: application/json");
require_once("db.php");
$start = $_GET['start'];
$length = $_GET['length'];
$sql = "SELECT * FROM clientes ORDER BY Cod_Convenio ASC;";
$resultado = $con->query($sql);
$resposta = array();
$resposta['recordsTotal'] = $length;
if (!$resultado) {
$resposta = array(
'status' => false,
'message' => 'Houve um pequeno problema...'
);
} else {
$resposta = array(
'status' => true,
'message' => 'Tudo ocorreu bem',
'data' => $resultado ->fetchAll()
);
}
//print_r($resposta);
echo json_encode($resposta);
exit;
?>