Multi part forms with Javascript
Andy Arikawa has a nice demonstration showing how multi page forms can be served up as a single page using some simple Javascript, resulting in a clearer form without loading times in between each page.
The concept is great, but the execution has a some subtle flaws—the most important of which is that browsers with Javascript support turned off (but CSS turned on) will not be able to use the form. The culprit is the use of a style="display: none;" attribute on the two divs that should initially be hidden. CSS supporting browsers will follow this rule blindly, even if Javascript is disabled and they will thus be unable to later reveal the hidden page content. The solution is to have all of the elements visible initially, then hide them with a piece of Javascript that executes when the page has loaded.
My other small gripe is the use of javascript: in the href part of the links to activate the toggle function. Dive Into Accessibility has the skinny on why this is a bad idea. Since the toggle links lead to other sections of the form, it makes sense to use the IDs of those sections as the link so that browsers without javascript will still “jump” to the section that is linked to. The Javascript itself can be placed in an onclick attribute (and duplicated in an onkeypress attribute for improved accessibility).
Taking the above points in to account, here’s a reworked version of Andy’s demonstration that should work just fine in browsers with Javascript support disabled.
Jay Small - 12th August 2003 13:13 - #
Anne van Kesteren - 12th August 2003 13:56 - #
I get a javascript error with IE5/Mac
eric scheid - 12th August 2003 14:11 - #
Simon and all,
Thanks for pointing out the flaws in my concept. :-)
I've updated the post to reference Simon's reworking, and unlinked the originals. (E-mail sent to you, Simon, to this effect).
As for the javascript error mentioned, it was an attempt to focus the first field of the form. Um, can you tell I'm not a JavaScript expert?
Arikawa - 12th August 2003 15:31 - #
The error happens on Opera 7/Win, too.
document.contactform is *not* guaranteed to give you the form object. The fact that it did back in Netscape 2, and does still in IE, is an undocumented (AFAICS) shortcut feature.
Instead, use document.forms.contactform, or document.forms['contactform']. This has the advantage of being clear when you pick a form name that clashes with one of the properties of the form object.
The same should be done with form elements. So use:
Andrew Clover - 12th August 2003 16:23 - #
Simon Willison - 12th August 2003 17:30 - #
Not sure if this is still a problem, but some time ago when I made a long form using a similar concept of hiding sections, some of the data within the form wasn't being sent on submitting.
I tracked it down to some browsers wrongly assuming that an input element that resides under an element set to display:none, will not be sent when the form is submitted.
At the time the way round it was to unhide all sections of the form as the submit button is pressed.
Dave Page - 13th August 2003 11:09 - #
Rob Hudson - 19th August 2003 19:11 - #
Elaine Nelson - 20th August 2003 18:37 - #
One drawback I see: if you validate the submitted data and there's errors, are you expecting them to start on the first page of the form again and click through until they find the incorrect forms?
Also, would you put the error messages on the individual pages (difficult) or all at the top? this would be far easier but wouldn't relate the errors to the pseudo pages
Peter Bowyer - 21st August 2003 17:09 - #
Peter Bowyer - 21st August 2003 17:20 - #
,mn,mn - 25th August 2003 19:42 - #
Mark - 28th August 2003 02:29 - #
vijay - 16th September 2003 06:42 - #
hemachander - 21st November 2003 10:08 - #
f - 18th April 2004 19:06 - #
f - 18th April 2004 19:06 - #
f - 18th April 2004 19:07 - #
f - 18th April 2004 19:07 - #
t - 2nd May 2004 15:08 - #
ewtrwertwe - 28th July 2004 16:49 - #
23 - 31st August 2004 17:10 - #
23 - 31st August 2004 17:10 - #
23 - 31st August 2004 17:19 - #
23 - 31st August 2004 17:19 - #
3dfdf - 29th September 2004 03:49 - #
sdvs - 31st March 2005 04:24 - #
I am looking at ways to cache most of a multi-part form through JavaScript. At the moment I am looking at a CGI-generated page with the 'top' form containing only hidden inputs. Using an iframe in this page I reference the first static HTML layout page with tables but no form. Using JavaScript and DOM I populate the static iframe elements (through onload event callback). To move on to the next layout page I intend to change the src of the iframe through DOM (perhaps using onunload event callback to copy the displayed data back into the top form).
This has several advantages to me, the CGI-generated page is tiny (mostly hidden input fields) while all the iframes and JavaScript are static and can be cached. I can add/delete rows to/from layout tables throught DOM and then add/delete elements to/from the top form on iframe's unload. (I have not been able to add child input elements to a tabledata object so that they are also elements of a containing form. So, by separating the form from the layout, I can have separate input elements in the (hidden) form and in the display table.
Andy Robb - 14th April 2005 10:32 - #
vbnbv - 25th May 2005 02:45 - #
chandra - 21st July 2005 08:05 - #
asd - 22nd September 2005 19:11 - #
d - 21st December 2005 11:16 - #
fgdd - 16th April 2006 17:33 - #
fgdd - 16th April 2006 17:33 - #
babu pal - 9th June 2006 06:19 - #
venkata - 17th October 2006 06:09 - #