Jump to content

Form sample code doesn't validate


Recommended Posts

just noticed that some code I was having trouble with ended up not validating. http://www.w3schools...put_type_hidden this is some sample code to showcase the hidden input types If you put in this code

 <?xml version="1.0" encoding="utf-8" ?> <!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"> <form action="form_action.asp" method="get">   Email: <input type="text" name="email" /><br />   <input type="hidden" name="country" value="Norway" />   <input type="submit" value="Submit" /> </form> <p>Click on the submit button, and the input will be sent to a page on the server called "form_action.asp".</p> </body> </html> 

you end up with errors like this

 Line 6, Column 3: character data is not allowed here   Email: <input type="text" name="email" /><br /> ? You have used character data somewhere it is not permitted to appear. Mistakes that can cause this error include: putting text directly in the body of the document without wrapping it in a container element (such as a <p>aragraph</p>), or forgetting to quote an attribute value (where characters such as "%" and "/" are common, but cannot appear without surrounding quotes), or using XHTML-style self-closing tags (such as <meta ... />) in HTML 4.01 or earlier. To fix, remove the extra slash ('/') character. For more information about the reasons for this, see Empty elements in SGML, HTML, XML, and XHTML.

and a few of these

 Line 6, Column 43: 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   Email: <input type="text" name="email" /><br /> ? 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>").

a few more errors that might be due to the hasty page I created but things like the first bit really don't validate when you use them in a real context and teh second one. I've been able to get them to work if you wrap them in a span or div. Someone was telling me that input can't be a direct child element of form but has to have a block level element above it.

Edited by wolfkin
Link to post
Share on other sites

A form can only contain block elements. That means <div>, <p>, <fieldset> and many others. Just wrap all the form contents inside a <div> or <fieldset> tag and there will be no validation problems.

Link to post
Share on other sites

I did notice that.. i think i put that in my post. My suggestion was just that the fact that this isnt in the tutorial might cause problems for other beginners. I was stuck on this for quite a while before I figured out the div wrap. I don't know if it's intentionally left out or not. it's just that nowhere on the forms page does it suggest that you need block level elements for properly formed (X)HTMLhttp://www.w3schools.com/html/html_forms.asp since it is the sample code for teaching I was just suggesting that maybe we put a div wrapper in there.

Link to post
Share on other sites

I have to agree with wolfkin, though. IMO, if you're going to teach somebody how to do something, you should teach them the right way. In HTML, that means writing well formed, semantic code. I realize it's up to the Refnes people to update the tutorials, but I just wanted to throw in my 2 cents.

Link to post
Share on other sites

fair enough FoxyMod. but I think there's a bit of a difference between leaving out a doctype and leaving out a div and not mentioning it. The doctype declaration could be rather confusing to read thru for beginners, I don't think the div would be so. Also in the course of the tutorials, the necessity for a DOCTYPE declaration is actually is mentionted not so with this example. But as I say.. if the omission was intentional (like the DOCTYPE omission) than I accept that and I'm done here. It was just an idea I had. Cheers.

Edited by wolfkin
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...