Retrieve workbook via AJAX request

Topics: User Forum
Aug 6, 2013 at 9:30 PM
Hi Everyone,

I'm using PHPExcel 1.7.9 with PHP 5.4.17 and I've got a script which will generate a workbook from a MySQL query. The script works fine and the browser returns an Excel file to download if I hit the script directly and pass values via GET but I've got checkboxes on a form which I need to pass values to the script via POST due to size.

I've got jquery doing this >
$("button#excel").click(function(event){
        event.preventDefault();
        var selectValues="range=";
        $selections = $('input:checkbox:checked');
        $selections.each(function(){
            selectValues += '\'' + $(this).val() +'\',';
        });
        
        $.ajax({
            url: "book.php",
            data: selectValues.substr(0,selectValues.length-1),
            method:'POST',
            success: function(response){
                window.location.href = response.url;
            }
        });
    });
Data is basically a concatenated list of UUIDs provided by checkboxes on an HTML form. The PHP script containts a MySQLI statement where data is bound as a parameter to an IN function in the query's where clause.

So what's supposed to happen is when a user clicks a button, all id's associated to a checked checkbox gets concatenated into a comma separated string. The script executes the query and builds the workbook which then gets fed back to the browser as a file to download but I can't get it to work! I tried several approaches and none seem to be working out and I'm running out of ideas. Would any of you have experience doing something like this? Any advice would be appreciated.

Thanks in advance!
Aug 7, 2013 at 11:38 AM
Can you determine more precisely where is the problem?
The Excel's file is properly generated, recorded? From your script, I'm guessing that the target of the Ajax request returns an url that allows to obtain the file. Is the url correct?
If you use it directly in the browser, you get the file?
Aug 7, 2013 at 4:03 PM
LWol,

Correct, because im using $_REQUEST['range'] I can pass a few UUIDs to the PHP script generating the Excel file properly. UUIDs are pretty long and if you provide several 100 you cap out via GET method.

The script I provide above in the $.ajax fails. It doesn't retrieve anything but providing the same information via the $.post oddly enough works, the only problem is I'm not sure how to handle the data on callback, it's creating an object without a URL.

I was able to create a workaround by adding a hidden form to the page with one hidden field "range" that I provide data to via jquery. The button then submits the form directly to "book.php" and the browser returns the file to download. It's totally hack and slash but the user can't tell the difference. It would be nice to properly implement the AJAX thing though.

I'll feedback if I find a solution, I think I just hit one of those walls after trying so many different approaches.
Aug 8, 2013 at 6:48 AM
The problem could well be in the back. What answers book.php when it is called by the ajax? Your code suggests JSON. Is recognized as such by jQuery (no datatype in the request, unless a change in global settings by default, it is "guessed")? Is it well formed?