単純なフォームを作成し、DatatablesjQueryプラグインを使用してその中にいくつかのデータを表示しています。データは、JSON形式で適切な結果を返すphpスクリプト(process.php)によって入力されています。フォームには、テキストボックスの値をprocess.phpに送信するボタンがあります。問題は、ボタンがクリックされたときにprocess.phpスクリプトによって受信された新しいデータでデータテーブルを更新/変更できないことです。
フォームのコード:
<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>
データテーブルを作成するために、次の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();
}
});
});
} );
process.phpスクリプト(正常に動作します)は次のとおりです。
<?php
$result="";
if (empty($_REQUEST["txtId"])) {
$result = '{"aaData":[["1","Surname1","Name1"]]}';
}
else {
$result = '{"aaData":[["2","Surname2","Name2"]]}';
}
print $result;
?>