Aktualisieren Sie Datentabellen (JQuery), wenn Sie auf die Schaltfläche klicken
Ich habe ein einfaches Formular erstellt und verwende das jQuery-Plugin für Datatables, um einige Daten darin anzuzeigen. Die Daten werden von einem PHP-Skript (process.php) ausgefüllt, das die richtigen Ergebnisse im JSON-Format zurückgibt. Im Formular gibt es eine Schaltfläche, die den Wert der Textbox an die process.php sendet.Das Problem ist, dass ich die mit dem process.php-Skript empfangenen neuen Daten nicht aktualisieren / ändern kann, wenn auf die Schaltfläche geklickt wird.
Der Code des Formulars:
<code><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> </code>
Zum Erstellen der Datentabelle verwende ich den folgenden JQuery-Code:
<code> $(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(); } }); }); } ); </code>
process.php Skript (funktioniert gut) ist:
<code><?php $result=""; if (empty($_REQUEST["txtId"])) { $result = '{"aaData":[["1","Surname1","Name1"]]}'; } else { $result = '{"aaData":[["2","Surname2","Name2"]]}'; } print $result; ?> </code>