Posting data to server-side script from datatable ajax...

Posting data to server-side script from datatable ajax...

LCharlierLCharlier Posts: 4Questions: 2Answers: 0
edited June 2023 in Free community support

I built a datatable-based app that displays the contents of a MySQL database table (server-side processed) that functions as expected. Since the database is very large however (>8 million records with over 330 fields), I built a front-end to the application where the user can select several search criteria thereby narrowing down the number of records to be displayed. Upon submission, my app takes the SQL corresponding to the search criteria and creates a WHERE clause to be applied to the SQL query that builds the table (using ssp.class.php). As I was already using the ssp helper class on the server side, I figured that this would be straightforward - submitting my WHERE clause as data posted to the server script via the datatable ajax routine already in use. On the back end, I merely changed the SSP::simple function call to instead be the SSP:complex function call - again, seemingly straightforward... or so I thought.

Before incorporating the changes to my front-end code to post the WHERE clause inside the ajax, I first tested-out the SSP::complex function on the server-side by hardcoding the WHERE clause in the function call - it works perfectly. Armed with this success, I modified my existing datatable ajax (for clarity, just the ajax section of the datatable definition is shown) to include posting the WHERE clause data as shown below (the commented-out line is what I used prior to adding the WHERE clause):

// Populate datatable with data based on search criteria
$('#populateTable').click
(
    function (e)
    {
        // Line below for test purposes only. In practice, whereClause will be derived from
        // search criteria obtained from search form
        whereClause = "provider_business_practice_location_address_state_name='CA'";

        table = $('#dataList').DataTable
        (
            {
                processing: true,
                serverSide: true,
                // ajax:       'fetchData.php',
                ajax:
                {
                    "type": "POST",
                    "url":  'fetchData.php',
                    "data":
                    {
                        "wc": whereClause
                    }
                },

Since I had already tested-out my application successfully - both without the change and with a WHERE clause hardcoded-in on the server-side, I was sure this would work fine. I was wrong... Strangely enough, every time I run the latter version, the app hangs. When I revert the code by commenting-out the change and uncomment the original code, everything works. Suspecting that the WHERE clause info wasn't posting (for some reason), I examined the payload info using my browser's developer tools and discovered that the posted data was being posted just fine! When I added debug to the server-side, sure enough - the posted WHERE clause was being received as expected. Unfortunately, even though everything seems to suggest that all should be working just fine, the app hangs every time as long as I post the WHERE clause info as shown above. When I remove the minor mod above, all works fine. Am I doing the posting wrong?

Please advise - thanks!

Lloyd

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769
    edited June 2023 Answer ✓

    Since you are changing form GET to POST did you set this command to pass the POST parameters?

    echo json_encode(
        SSP::complex( $_POST, ... )
    );
    

    Kevin

  • LCharlierLCharlier Posts: 4Questions: 2Answers: 0

    Wow - that's twice in three days you've pointed out what should've been obvious to me! I never thought to look at the SSP function params. I've been beating myself up trying to figure out what I was doing wrong by focusing on the ajax call syntax itself - especially the way it was behaving. It works perfectly now - thanks a lot for the quick assistance!

    Lloyd

Sign In or Register to comment.