How to pass a PHP form variable to server processing for use in WHERE clause?
How to pass a PHP form variable to server processing for use in WHERE clause?
I have a form in my application where you select a "customer" and a "startdate" and "enddate".
When the user clicks on "submit", I want to pass these 3 POST variables through to datatables to be used in a WHERE clause to filter the results.
I've tried a bunch of things with no success. Using Firefox I can see the variable "formCustomer" in the "params".
Here's my code but how do I read the "formCustomer" variable in the page "scripts/server_processing_stock.php"?
<script type="text/javascript" language="javascript" class="init">
$.extend( $.fn.dataTable.defaults, {
responsive: true
} );
$(document).ready(function() {
$('#stock_report').dataTable( {
"language": { "infoFiltered": ""},
"order": [[ 1, "desc" ]],
"processing": true,
"serverSide": true,
"ajax": {
"type": 'POST',
"url": 'scripts/server_processing_stock.php',
"data": {
formCustomer: '<?php echo $_POST["sel_customer"] ?>',
}
}
} );
} );
</script>
This question has an accepted answers - jump to answer
Answers
Use
ajax.data
as a function rather than a static object. That way you can read the values from the DOM and insert them into the request object on-the-fly.Allan
I tried that too. The variables seem to be passed, BUT, in the ssp.class.php file, how do I read them in and use them in a where clause like the following:
I try $customerid = $_POST['formCustomer']; etc to no avail.
So, in my server_processing.php I AM getting the variables as $_POST type variables, but the $where clause passed through the
isn't working.
The SSP class is designed to be super simple. It will require modifications if you want to perform some more complex conditions beyond just the simple global and column options of DataTables.
Have a look at the
complex
static method however, it also will require modification to match your needs.Allan
Hi Allan, yes, I'm using the "complex" example above and it is now filtering.
One problem though is that when the results are returned to the table instead of the default "showing 10" records, it shows ALL 16 records with 2 pages in pagination and says "Showing 1 to 10 of 16 entries" when in fact it is showing ALL records.
Are you using the
whereResult
orwhereAll
option of the complex method. Sounds like you wantwhereAll
.Allan
I try both with the same result
It's as though anything above 10 records breaks pagination.
Can you link to the page and show me the code please?
It's behind a login but I can send you credentials via email with steps to the page in question and send you the code.
Seeing as this particular datatable is for a report, I turned off pagination, however, there seems to be a bug with the "complex" PHP file and pagination.
I've just tried this with my basic server-side processing example:
And it seems to work as expected (including specifically the paging).
Allan
Hi Allan
I'm using the following:
Running against "ssp.class.php" in your static link to GitHub.
Allan: I found the issue but not sure how to fix it. The issue is with the <script> code on the calling page with AJAX. The issue is with AJAX and DATA and how it's constructed.
It brings back data and displays it BUT all records show but pagination shows pages etc and also search and sort at headers doesn't work either.
What am I doing wrong here?
Here is my code.
Just found it. As soon as I removed
Everything works!?
Your
server_processing_activity.php
script is expecting GET parameters then (line 73 in the PHP script above uses $_GET confirming this).Allan
Thanks Allan, I removed the "type": 'POST' and changed the parameters in the server_processing_activity.php to be $_GET and everything is running fine now.
Many thanks...what a great product!