Click here to get back home

Form and nested elements

 HomeNewsGroups | Search | About
 comp.infosystems.www.authoring.html    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Form and nested elements Markus Ernst 11-10-2004
Get Chitika Premium
Posted by Markus Ernst on November 10, 2004, 11:57 am
Please log in for more thread options
Hi

I have a validation problem with a form and nested divs. I understand what
the problem is, but I don't see how to fix it. This is my normal page
structure, and it validates:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Test</title>
</head>
<body>
<div>
</div>
<div>
<div>
<form name="form1" action="">
<p>Some form elements</p>
</form>
</div>
<div>
<form name="form2" action="">
<p>Some form elements</p>
</form>
</div>
</div>
</body>
</html>

Now the form has some additional elements in the first div:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Test</title>
</head>
<body>
<div>
<form name="form1" action="">
<p>Some form elements</p>
</div>
<div>
<div>
<p>Some more form elements</p>
</form>
</div>
<div>
<form name="form2" action="">
<p>Some form elements</p>
</form>
</div>
</div>
</body>
</html>

And due to improper nesting this does not validate. I tried the same thing
with a layout table, it does not validate either (neither in strict nor in
transitional mode). Note that form 2 is the reason why I can't span the form
over the whole body, which would make it validate without a problem.

Now in the early days I learnt that forms could begin and end inside or
outside any element, regardless of the layout or table structure. Is this
just outdated, or is there a possibility to declare a form as cross-element?

--
Markus




Posted by Neal on November 10, 2004, 9:47 am
Please log in for more thread options
wrote:

> <body>
> <div>
> <form name="form1" action="">

You've opened a div and a form.

> <p>Some form elements</p>
> </div>

Now you've closed the div. Therefore the form is also closed.


Posted by Harlan Messinger on November 10, 2004, 10:54 am
Please log in for more thread options

> Hi
>
> I have a validation problem with a form and nested divs. I understand what
> the problem is, but I don't see how to fix it. This is my normal page
> structure, and it validates:
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
> "http://www.w3.org/TR/html4/strict.dtd">
> <html>
> <head>
> <title>Test</title>
> </head>
> <body>
> <div>
> </div>
> <div>
> <div>
> <form name="form1" action="">
> <p>Some form elements</p>
> </form>
> </div>
> <div>
> <form name="form2" action="">
> <p>Some form elements</p>
> </form>
> </div>
> </div>
> </body>
> </html>
>
> Now the form has some additional elements in the first div:
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
> "http://www.w3.org/TR/html4/strict.dtd">
> <html>
> <head>
> <title>Test</title>
> </head>
> <body>
> <div>
> <form name="form1" action="">
> <p>Some form elements</p>
> </div>
> <div>
> <div>
> <p>Some more form elements</p>
> </form>
> </div>
> <div>
> <form name="form2" action="">
> <p>Some form elements</p>
> </form>
> </div>
> </div>
> </body>
> </html>
>
> And due to improper nesting this does not validate.

Right. *Everything* in HTML is nested. No partial overlapping allowed.

>I tried the same thing
> with a layout table, it does not validate either (neither in strict nor in
> transitional mode). Note that form 2 is the reason why I can't span the
form
> over the whole body, which would make it validate without a problem.
>
> Now in the early days I learnt that forms could begin and end inside or
> outside any element, regardless of the layout or table structure. Is this
> just outdated, or is there a possibility to declare a form as
cross-element?

ISTR that if you wanted to lay out a form with a table in an old Netscape
version, you kind of had to put the FORM tags just inside the TABLE tags or
something like that, and IE didn't reject it when you did it that way.
Anyway, it's definitely wrong in current HTML.



Posted by Markus Ernst on November 10, 2004, 6:01 pm
Please log in for more thread options
Harlan Messinger wrote:

> ISTR that if you wanted to lay out a form with a table in an old
> Netscape version, you kind of had to put the FORM tags just inside
> the TABLE tags or something like that, and IE didn't reject it when
> you did it that way.

Actually both IE and Mozilla do not reject my page with the wrongly nested
form; they display it the way I want ("correctly" would not be the
appropriate term here).

> Anyway, it's definitely wrong in current HTML.

I was afraid that would be the only possible answer, thank you.

--
Markus




Posted by Lachlan Hunt on November 11, 2004, 4:34 pm
Please log in for more thread options
Markus Ernst wrote:

<snip>Markup example with improperly nested form</snip>

> And due to improper nesting this does not validate.
> Note that form 2 is the reason why I can't span the form
> over the whole body, which would make it validate without a problem.

There are never any occasions where you need improper nesting of
elements like you had, and doing so is exceptionally bad because, even
though current browsers can handle it as tag soup, it is handled very
differently in different browsers, and sometimes causes very different
results. So, you can never be sure what you'll get with improperly
nested elements in different browsers, unless you test them all, but it
should never be done anyway.

I suggest you think a little more about your document structure. They
were two seperate forms, so mark them up as that. If half of form one
was appearing in a seperate section, then that's most likely for
presentational reasons, I can't imagine any structural or semantic
reason to do so. You should also be able to remove a few of those
<div>s, and just apply styles directly to the form element. You should
generally avoid overusing divs, since they have no semantics, and often
serve to unnecessarily bloat your code, so use them sparingly.

> Now in the early days I learnt that forms could begin and end inside or
> outside any element, regardless of the layout or table structure. Is this
> just outdated

It's not just outdated, it was never valid. It just means you were
taught by an ignorant teacher, or from a poorly written book.

--
Lachlan Hunt
http://lachy.id.au/
http://GetFirefox.com/ Rediscover the Web
http://SpreadFirefox.com/ Igniting the Web


Similar ThreadsPosted
XHTML Validation fails with nested block elements. January 10, 2005, 5:38 am
Visibility of form elements December 3, 2004, 8:01 pm
form elements which should not be submitted March 9, 2006, 3:17 pm
INPUT type="image" and Form Elements October 6, 2004, 12:04 pm
Using multiple button type="submit" elements within a form April 15, 2005, 4:24 pm
Problems and questions regarding HTML form SELECT elements September 18, 2007, 6:38 pm
Nested forms August 20, 2004, 9:53 am
nested frames October 13, 2005, 10:21 pm
RE: nested frames October 15, 2005, 5:08 am
Nested OBJECT frustration October 6, 2005, 8:26 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap