Forum Moderators: open

Message Too Old, No Replies

Why can't i use an [ ] in a id attribute when validate HTML

         

BlackDex

11:16 am on Dec 30, 2005 (gmt 0)

10+ Year Member



Hello ppl,

why can't i use a [] in a id attribute.

example:

<input type="text" id="myform[Text1]" name="myform[Text1]" />

The W3C Validator says that the [ char can't be used.But why?
It is very handy in PHP to let it return an array in the POST or GET Arguments.

If anyone has a reason or something or can explain this to me.
Or should i just use it and ignore this warning/error?

Thx in advanced.

Robin_reala

12:15 pm on Dec 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Because they're of type 'name' [w3.org]. As to whether you should use it, well, that's your decision. But it's be a better idea to choose some other convention that you could transcode on the server. For example, you could specify that an underscore is equivalent to brackets: myform[Text1] could become myform_Text1 which your server-side script could tokenise.

BlackDex

12:51 pm on Dec 30, 2005 (gmt 0)

10+ Year Member



That is what i do now..
I Use the _ and then loop through the _POST array in php en check for the value before the _ and create an array of the value beheind the _.

Thx for the awnser :)

jetboy

2:41 pm on Dec 30, 2005 (gmt 0)

10+ Year Member



If you look at the W3C HTML4 specs for forms, you'll see the Name attribute listed as CDATA, which *can* have square brackets in it.

However, in XHTML1.0 it was changed to NMTOKEN, a type which *can't* have square brackets in.

There's no rule that says your ID and Name have to be the same, so in HTML4 you can quite happily go with something like:

<select multiple id="myname" name="myname[]">

I think I'm right in saying that a similar naming convention with an XHTML1.0 DOCTYPE doesn't trip the W3C's validator, even though it's technically incorrect.