Question

Is it possible to pass a PHP phpexcel object into a page's HTML so I can pass it back to the server with a form POST or in an ajax call using jquery or something? I create the object when I'm generating an html table view of the data and I want them to be able to click on a button and download it as a document if they want it, but I want to be able to use the object I already created to produce the download document for them.

Can I pass the object to the client in the HTML and then pass it back to the server?

Was it helpful?

Solution

Typically what I do in these scenarios is one of the following:

1) Generate and save the information as a document at the same time as I generate the page to display to the user. Then, when they want to download it, I link directly to the file I just created. This is appropriate when it's a low volume page that maybe one person or a handful of people will be accessing once a day, and they have a high probability of downloading the file every time they use it. This is usually the scenario I find myself if when generating these sorts of documents. Just make sure you manage your documents so they don't pile up, probably by overwriting existing documents with the most recent document every time you generate it, or otherwise by deleting old documents.

2) When they click to download, just re-generate the data with the same queries. This is appropriate if the queries are fast and the data doesn't change much, or it doesn't matter if the data that they download is slightly updated from the data they see on the page.

3) When generating the data for the page, save it to the user $_SESSION, which should (for most objects, at least) serialize it and unserialize it appropriately for future access. If PHPExcel doesn't handle this well, you'll probably want to create your own class that inherits from PHPExcel and use __sleep() and __wakeup() to appropriately manage any details you need to make sure it serializes and unserializes appropriately. Then when you make your call to download the data, you can use the data in memory just like you had just created it. You may want to actively manage your user session to make sure it doesn't sit in there taking up a bunch of memory each time the user requests a page, if it's a fair amount of data.

The last option is probably the most "correct" approach, and is likely to be appropriate for most scenarios. A serialized PHP object has no business being stored in an HTML form to be passed back to the server, at least not under any normal circumstances.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top