Forum Moderators: open

Message Too Old, No Replies

Validating Checkbox Inputs

checkbox,validation,xhtml

         

pmmenneg

2:58 am on Nov 18, 2009 (gmt 0)

10+ Year Member



I have an XHTML 1.1 strict document that has a number of checkboxes in it that are all linked together (the user can select multiple options), ie:

- option 1
- option 2
- option 3

These have id and name tags of id="option[1]", id="option[2]", id="option[3]" and name="option[1]", name="option[3]", name="option[3]" so that I can use the html label tag and attach a label to each checkbox via for="option[1]", etc.

This seems to work perfectly in terms of passing me an array of values upon submission, and the labels are all properly tied to the checkboxes... my problem is validation.

I know it's not a big deal, but would love this doc, as complicated as it is, to validate. The error I get is:

character "[" is not allowed in the value of for

If I remove the [] from the for attribute of the labels, they don't tie in with the inputs...

Any suggestions?

Thanks!

D_Blackwell

3:30 am on Nov 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What am I am missing thatyou need the brackets at all? Match id and for by removing brackets from all. ?

<!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></title>
<meta http-equiv="content-style-type" content="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style type="text/css" media="screen">
form {
width: 200px; margin: 5%;
}
</style>
</head>
<body>
<form action="">
<fieldset>
<legend>Check Boxes</legend>
<input type="checkbox" id="option1" />
<label for="option1">Options 1<br /></label>
<input type="checkbox" id="option2" />
<label for="option2">Option 2<br /></label>
<input type="checkbox" id="option3" />
<label for="option3">Option 3</label>
</fieldset>
</form>
<!--##########
I have an XHTML 1.1 strict document that has a number of checkboxes in it that are all linked together

(the user can select multiple options), ie:

- option 1
- option 2
- option 3

These have id and name tags of id="option[1]", id="option[2]", id="option[3]" and name="option[1]",

name="option[3]", name="option[3]" so that I can use the html label tag and attach a label to each checkbox via for="option[1]", etc.

This seems to work perfectly in terms of passing me an array of values upon submission, and the labels are all properly tied to the checkboxes... my problem is validation.

I know it's not a big deal, but would love this doc, as complicated as it is, to validate. The error I get is:

character "[" is not allowed in the value of for

If I remove the [] from the for attribute of the labels, they don't tie in with the inputs...
-->
</body>
</html>

pmmenneg

3:51 am on Nov 18, 2009 (gmt 0)

10+ Year Member



Well, the checkboxes are created via PHP, and can range from two to seven in number. If I don't have these checkboxes being passed as an array to the validation script, checking them becomes non-trivial in that I need to then fiddle around with the different input ids that have been passed...

D_Blackwell

5:34 am on Nov 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the checkboxes are created via PHP

Then would it be practical, or even possible, to replace brackets with ascii characters in the HTML?

It sounds like you will have to handle as a PHP solution. It's a PHP problem.?

pmmenneg

4:03 pm on Nov 18, 2009 (gmt 0)

10+ Year Member



Hey DBlackwell, thanks for the response! Let me know what you think of this...

First, here's how things are happening right now:

User hits form page, php goes to db and grabs options, display them to the user.

User selects a few of these options, which get sent via POST to php. PHP then grabs the options, which were passed as an an array because of the [], and can easily loop through them and insert them back into db.

If I go with some other characters, say id="option1" instead of id="option[1]", it's no problem on the generation side to loop through and do this of course, but on the processing side, unless I am missing something, I'm left doing something along these lines:

- Get POST data
- Loop through POST array, pattern testing each passed element to see if it starts with 'option'. Store matches in an array of items that do have this pattern
- Foreach this temp array and store the values in the db.

Sound about right?

Fotiman

5:13 pm on Nov 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Since it's the name value that the PHP scripts care about, why not just modify your page to generate like this:

<input name="option[1]" id="option1" ...
<input name="option[2]" id="option2" ...
<input name="option[3]" id="option3" ...

This way, when the form is submitted, you will still have an "option" array, and on the page each input can be referenced with the id.

pmmenneg

5:23 pm on Nov 18, 2009 (gmt 0)

10+ Year Member



*facepalm* of course... forgot about the name attribute... geez.

Thanks Fotiman!