I really can't figure out how to pass a form to datatables.
I'm trying to have a select form, this form will be use as my WHERE clause to my query on database to populate the datatables. Here's my current code.
Index.php
<form method="POST" id="frm">
<select name="selectplace">
<option value="PLACE 1">PLACE 1</option>
<option value="PLACE 2">PLACE 2</option>
<option value="PLACE 3">PLACE 3</option>
</select>
<button type="submit" name="submitPlace">SUBMIT</button>
<div class="table-responsive">
<table class="table table-bordered table-striped text-center" id="place-table">
<thead>
<tr>
<th>PLACE #</th>
<th>PLACE NAME</th>
<th>TOTAL VISITORS</th>
</tr>
</thead>
<tfoot>
<tr>
<th>PLACE #</th>
<th>PLACE NAME</th>
<th>TOTAL VISITORS</th>
</tr>
</tfoot>
</table>
</div>
JQUERY for datatable
$(document).ready(function() {
$('#place-table').DataTable({
"ajax": {
url: "json.php",
"dataSrc": "",
"data": function(d) {
var frm_data = $('frm').serializeArray();
$.each(frm_data, function(key, val) {
d[val.name] = val.value;
});
}
},
columns: [{
data: 'place_id',
}, {
data: 'place_name',
}, {
data: 'total_visitor',
}]
});
});
</script>
json.php
This where I want to pass the form so I can use it as my WHERE clause
<?php
$selectedplace = $_POST['selectedplace'];
$sql = "SELECT * FROM placestable WHERE $selectedplace";
$result = mysqli_query($conn, $sql);
$data = array();
while($row = mysqli_fetch_assoc($result)) {
$data[] = array(
"id"=>$row['id'],
"place_name"=> $row['place_name'],
"total_visitor"=> $row['total_visitor'],
);
}
echo json_encode($data); //before was: echo json_encode(array('data' => $data));
?>