Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't form nested in p validate as XHTML

Tags:

forms

xhtml

Take for instance this XHTML snippet:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head>   <title>A webpage</title> </head> <body>   <p>     <form action="something.php" method="get">       <input type="submit" value="Hello"/>     </form>   </p> </body> </html> 

The tree should be valid, however this won't parse correctly in a browser.

like image 243
Duncan Avatar asked Jun 20 '09 17:06

Duncan


People also ask

Can we use form tag inside form tag in HTML?

No, nested forms are forbidden. This means A FORM has a mandatory start tag, mandatory end tag and can contain anything in %block or SCRIPT, except other FORMs.

Can form tag be nested?

There can be several forms in a single document, but the FORM element can't be nested.

Can we use P tag inside form?

Not it is not, p is a block element, but cannot contain other block elements, only inline ones.

Is form xhtml tag?

The form tag is used to delimit the start and stop of a form element and serves as a container for form controls (fields). Control is a technical term which refers to the various elements that can be used inside a form to gather information.


2 Answers

Look at the error messages that you get when you try that with http://validator.w3.org

Apart from a warning that you haven't specified a character encoding (and that it's therefore assuming UTF-8), the main error is that a <p> isn't allowed to contain non-inline content. You can either remove the <p> and </p> completely, or, move them inside the <form>.

As for 'why', it's because that's how it's defined in the schema which defines what is and what is not valid XHTML. If you look at this section of the XHTML definition you'll see that <p> is only allowed to contain text or 'inline' (not 'block') tags. However a <form> counts as 'block' content not as 'inline' content.

In other words, a form can contain paragraphs, but a paragraph cannot contain forms.

like image 67
ChrisW Avatar answered Sep 19 '22 12:09

ChrisW


According to this, because:

Line 8, Column 44: document type does not allow element "form" here; missing one of "object", "ins", "del", "map" start-tag ✉ The mentioned element is not allowed to appear in the context in which you've placed it; the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. This might mean that you need a containing element, or possibly that you've forgotten to close a previous element.

One possible cause for this message is that you have attempted to put a block-level element (such as "p" or "table") inside an inline element (such as "a", "span", or "font").

Line 9, Column 40: document type does not allow element "input" here; missing one of "p", "h1", "h2", "h3", "h4", "h5", "h6", "div", "pre", "address", "fieldset", "ins", "del" start-tag input type="submit" value="Hello" The mentioned element is not allowed to appear in the context in which you've placed it; the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. This might mean that you need a containing element, or possibly that you've forgotten to close a previous element.

One possible cause for this message is that you have attempted to put a block-level element (such as "p" or "table") inside an inline element (such as "a", "span", or "font").

like image 24
i_am_jorf Avatar answered Sep 20 '22 12:09

i_am_jorf