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

I am trying to design a CGI application. The plan is to have three popup menus and a text area. I don't generate the text area until an item has been selected from the lowest menu. I don't generate the lowest menu until something has been selected from the middle menu and so on up. My problem is that if I have everything generated and the user then changes something in the top or middle menus, I need to avoid generating the lower level stuff. The CGI documentation mentions an OnChange event that supports JavaScript, but (a) I don't want to learn JS, (b) I want my code to run where JS is disabled. The only plan I have is to create a hash of the menu values and compare these every time. It seems inelegant to me. Is there a better way do do it?

Regards,

John Davies

Replies are listed 'Best First'.
Re: CGI: which field has changed?
by CountZero (Bishop) on Feb 26, 2011 at 17:16 UTC
    If you cannot use JS, then you will have to regenerate the page every time a change in one the menus is made by the user.

    The http-protocol being essentially stateless, means that you will have to jump through a lot of hoops to "remember" what the previous state of the page was. Session-management springs to mind in that respect, but you can also keep the previous values of the menu-choices in some hidden fields.

    Using hidden fields, it is easy to check which choices have changed and regenerate the page.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Yes, I was expecting to have to regenerate, but since there are only four fields plus two or three buttons, I wasn't overly concerned. I read about hidden fields in the CGI docs, but couldn't envisage an application. Thanks to you, I can now! :-)

      Regards,

      John Davies

        This actually proved harder than I thought. Partly it was because I did something stupid (CGI: retaining textarea contents in hidden field), but even with this fixed, things didn't work as I expected. My code is now working as I want, but I have the following two lines in my code:

        print hidden(-name => "Old" . $param , -value => $value); param( -name => "Old" . $param , -value => $value);

        I can't find any documentation that indicates that the second line should be needed, but if I leave it out, the values don't get saved. I therefore report this in case anyone else runs into the same problem. Of course, if this should need only one line and I'm doing something else daft, I'd love to know.

        Regards,

        John Davies

Re: CGI: which field has changed?
by Anonymous Monk on Feb 26, 2011 at 14:11 UTC

      I can't see immediately whether or not his will do what I want. Going quickly through the tutorial, this, despite what it says, is the first reference I noticed to use of JS. It seems to indicate that JS will be used for OnWhatever events and for other, unspecified, validation. As I said in the OP, I want to avoid JS, and I fear that this will use JS in ways that I can't predict and will mean that I have to unpick so much that my original plan of a hash would be simpler. If there is more that I should have gleaned from the docs, any pointers would be most welcome.

      Regards,

      John Davies