Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a CGI script used to retrieve or alter values of database tables and then output the results by generating a table in html.

Using CGI.pm, I was able to store the previous selection each time a new request is made. However, I was unable to achieve a cummulative retention of the results.

eg:

1st request is to retrieve all entities of Column 1

2nd request made to retrieve an entry in (2,3)

After the 2nd request, the entry(2,3) is shown and Column 1 values will be gone since the page has been refreshed.

I am showing the request form and the corresponding table on the same HTML. One way of doing this is to use hidden field to store the current results and pass it together with the next request, but this method is cumbersome when I have a 10X200 table

Any help will be greatly appreciated.

Replies are listed 'Best First'.
Re: Creating persistent table with CGI.pm
by mirod (Canon) on Feb 24, 2001 at 16:08 UTC

    There are several ways to do this:

    • Store the previous queries in a hidden field, then you can run all of them, plus the new one every time you want to generate the table. Actually I don't know how you build queries but for security reasons I would not store the actual query but the parameters you used to generate it, or anything that allows to rebuild the query safely. Of course that means that you have to run several queries to build the 2cd page, so performance might be an issue.
    • Store the results of the first request in a file (make sure you generate a unique filename as described in "unique" filename?) and pass the filename in a hidden field. Of course this is slightly insecure as a user can change the filename.

    I don't know Apache::Session but I suspect you might be able to use it too.

Re: Creating persistent table with CGI.pm
by binary* (Novice) on Feb 24, 2001 at 20:20 UTC
    Another way to do this is store your information in cookies. Go here: Cookie Maker. Be aware of the size limitations though: 4k max size of any one cookie, max of 20 cookies per domain.
Re: Creating persistent table with CGI.pm
by wardk (Deacon) on Feb 25, 2001 at 03:07 UTC

    Another method might be to dump the data into a file, record the filename in a cookie/hidden field and reload from there.

    Since you have the power of a database, perhaps you could store the data in a temp table instead of the datafile. this would provide more power than a flatfile.

    good luck!

Re: Creating persistent table with CGI.pm
by sierrathedog04 (Hermit) on Feb 25, 2001 at 14:57 UTC
    CGI.pm enables you to store a query object's state to a file for later retrieval. The use of this feature should solve your problem.

    To store a query object to a file you should use CGI.pm's object-oriented style. Create a uniquely-named query object for each form you submit. Thus at the top of each form handler you might say something like: $myQueryObject = new CGI;.

    You will need to find out the name of the user under which your CGI script runs. Let us say that this user is named 'web'. Then, at the end of your form handler open up a filehandle to a directory in which 'web' has read/write privileges. Then save the query object using that filehandle, e.g. $myQueryObject->save(FILEHANDLE).

    If in a subsequent form handler you need to retrieve a value from a previous form, simply create a new query object from a filehandle that points to the file where you saved the previous query object, as in: $myPreviousQuery = new CGI(INPUTFILE);.

    Finally, you can retrieve any parameter from the previous query object by using CGI.pm's param() function.

    I have found that when opening up a previous query object it is better to use a reference to the file handle, e.g. $myPreviousQuery = new CGI(\*INPUTFILE);.