Server Side - Export to CSV

Server Side - Export to CSV

CaldagaCaldaga Posts: 24Questions: 6Answers: 0

I'm working on exporting to CSV with server side processing. I created the following custom button:

{
                    text: 'Export to CSV',
                    action: function ( e, dt, node, config ) {
                        var data = table.ajax.params();
                        str = JSON.stringify(data);
                        
                        $.ajax({
                            type: 'POST',
                            url: 'exporttocsv.php',
                            data: {json: JSON.stringify(json_data)},
                            dataType: 'json'
                        });
                        
                    }
}

For some reason when I press this button exporttocsv.php doesn't open. This button results on results.php which is in the same directory as exporttocsv.php. If I type in the full url to exportocsv.php it comes up just fine. Any ideas?

Answers

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    That's an interesting button :smile: Had to check it out myself and built this example:
    http://live.datatables.net/zepisile/1/edit

    I'm not sure what json_data is so I changed it to data. In my case the button performs a post to exporttocsv.php with the table ajax parameters. It fails with 403 Forbidden. Of course I don't have that script and expected a 404 but the Datatables JS BIN environment responds with 403. If I change the URL to /ssp/server_processing.php , which is different than the original AJAX URL, I get a 200 response with the JSON data.

    exporttocsv.php doesn't open. This button results on results.php

    What are you expecting to happen?

    Are you expecting a new page or table to open?

    Kevin

  • CaldagaCaldaga Posts: 24Questions: 6Answers: 0

    Right now my initial goal is to get exporttocsv.php to open a new page and this is the code in the php file:

    <html>
    <?php
        data = json_decode($_POST['json']);
        echo var_dump(data);
    ?>
    </html>
    

    Once I get the data over there and can see what format its in and how to access it I will get rid of the html and end up building a SQL query to create a CSV and download it to the client.

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    I don't think an AJAX request will open a new window. But you might be able to see what you want by using the browser's developer tools and looking at the Network tab. I can see what's returned from server_processing.php in my example above.

    Kevin

This discussion has been closed.