Ho creato un modulo semplice e sto utilizzando il plug-in jQuery di Datatables per visualizzare alcuni dati al suo interno. I dati vengono popolati da uno script php (process.php) che restituisce i risultati corretti nel formato JSON. Nel modulo, c'è un pulsante che invia il valore della casella di testo al process.php. Il problema è che non riesco ad aggiornare/modificare datatable con i nuovi dati ricevuti dallo script process.php quando si fa clic sul pulsante.Dati di aggiornamento (JQuery) quando si fa clic sul pulsante
Il codice del modulo:
<form name="myform" id="myform" action="" method="POST">
<label for="id">Enter an id:</label>
<input type="text" name="txtId" id="txtId" value=""/>
<input type="button" id="btnSubmit" name="btnSubmit" value="Search">
</form>
<div id="results">
<table class="display" id="example">
<thead>
<tr>
<th>id</th>
<th>Surname</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<!-- data goes here -->
</tbody>
</table>
</div>
Per creare DataTable, sto utilizzando il seguente codice JQuery:
$(document).ready(function() {
var oTable = $('#example').dataTable({
"sPaginationType": "full_numbers",
"iDisplayLength": 10,
//"bServerSide": true,
"sAjaxSource": "process.php"
});
$("#btnSubmit").click(function(){
$.ajax({
type: "POST",
url: "process.php",
data: 'txtId=' + $("txtId").val(),
success: function(result) {
oTable.fnReloadAjax();
oTable.fnDraw();
}
});
});
});
sceneggiatura process.php (funziona bene) è:
<?php
$result="";
if (empty($_REQUEST["txtId"])) {
$result = '{"aaData":[["1","Surname1","Name1"]]}';
}
else {
$result = '{"aaData":[["2","Surname2","Name2"]]}';
}
print $result;
?>
** mai ** creare JSON utilizzando le funzioni di stringa. PHP ha 'json_encode()'.Nel tuo caso, useresti 'echo json_encode (array ('aaData' => array (array ('1', 'Cognome1', 'Nome1'))));' – ThiefMaster
Sì, so della funzione json_encode() . Lo script process.php di cui sopra è stato scritto per motivi di semplicità. Grazie comunque, ThiefMaster! – dimmat