I created a simple form and I use the jQuery datatables plugin to display some data. The data is populated with a php script (process.php) that returns the correct results in JSON format. The form has a button that sends the value of the text field to the process.php file. The problem is that I cannot update / modify the datatable with the new data received by the process.PHP script when the button is clicked .
Form 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> </tbody> </table> </div>
To create a datatable, I use the following jQuery 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(); } }); }); } );
process.PHP script (works great):
<?php $result=""; if (empty($_REQUEST["txtId"])) { $result = '{"aaData":[["1","Surname1","Name1"]]}'; } else { $result = '{"aaData":[["2","Surname2","Name2"]]}'; } print $result; ?>
jquery jquery-datatables
dimmat
source share