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

Brethern

I'm reworking someone else's Perl code, building CGI scripts (using CGI::Pretty). He liberally used multiple <FORM> elements in a page, a practice which was previously unfamiliar to me. Sometimes these FORM elements are nested. I'm struggling to understand the existing code.

Why use multiple FORMs? OK, they may have different 'action' tags, pointing to other CGI scripts. But most of the examples I've run across don't. So why use multiple FORMs?

When there are multiple FORMs on a page, each with its own SUBMIT, do all the param's on the page get passed? or just the ones in the FORM that encloses the SUBMIT? Is the answer different for hidden params? Is the answer the same for both GET and POST? Is the answer the same when FORM elements are nested?

Are there any suggested best practices for multiple FORMs? Are there gotcha's in using multiple FORMs?

thanks
throop

  • Comment on Multiple and Nested <FORM>s in CGI - HTML

Replies are listed 'Best First'.
Re: Multiple and Nested <FORM>s in CGI - HTML
by technojosh (Priest) on Aug 17, 2007 at 15:31 UTC
    Not sure about nested forms, but as far as multiple forms on a page:
    • Submit should only send the params for the form it belongs to.
    • Hidden params behave the same as visible ones, in that they get sent with the form they belong to.
    • Those answers are the same for GET and POST
      > Submit should only send the params for the form it belongs to.

      So is there a straightforward way to make the params in the other forms persistent?

        Are you trying to retain values from other params in other forms on the page once one is submitted?
Re: Multiple and Nested <FORM>s in CGI - HTML
by dekimsey (Sexton) on Aug 17, 2007 at 16:08 UTC
    AFAIK nested forms are not allowed, they behavior is unreliable and the w3c validator I believe will choke on them (regardless of the doctype). The biggest concern is the fact that they are _unreliable_, my suggestion would be to break them out into separate forms. There are no gotcha's in multiple forms per page, just make sure your tags are closed properly and you'll be fine.

      The HTML 4.01 definition for the FORM element is:

      <!ELEMENT FORM - - (%block;|SCRIPT)+ -(FORM) -- interactive form -->

      I don't know if that prevents FORM children or FORM descendants, but I believe the intent is the latter, making nested FORMs illegal in HTML.