hybrid kill3r Posted March 13, 2010 Share Posted March 13, 2010 I need a script that loops through all the checkboxes in the form, determines which ones are not checked, and sets the value of those checkboxes to 0. Link to comment Share on other sites More sharing options...
jeffman Posted March 13, 2010 Share Posted March 13, 2010 Your form object has an elements property. It's an array of all the form elements in your form. You can loop through that and test for inputs. If you find one, test for checkboxes. If you find a checkbox, test the checked property.That means you have three nested if-structures nested in a for-loop.You can simplify things if you use form.getElementsByTagName("input"). That will return a collection of input elements only, leaving out the select elements and so on. Loop through that collection that is returned. You still need to test for type=="checkbox", but now you only need two if-structures. Link to comment Share on other sites More sharing options...
hybrid kill3r Posted March 13, 2010 Author Share Posted March 13, 2010 This is the javascript code that I came up with. function checkBoxes(){ //var permission = document.getElementById('contact-form'); var checkboxes = document.getElementsByName("group"); for(i = 0; i < checkboxes.length; i++){ if(checkboxes[i].checked == false){ checkboxes[i].value = 0; } } } This is my form(Generated by Database REcords) <table align='center' cellspacing='1' cellpadding='3' border='0'> <tr> <td align='right' width='30%'>Group Name</td> <td align='center' width='10%'>Read</td> <td align='center' width='10%'>Create</td> <td align='center' width='10%'>Modify</td> <td align='center' width='10%'>Attach</td> <td align='center' width='10%'>Comment</td> <td align='center' width='10%'>Delete</td> <td align='center' width='10%'>All/None</td> </tr><tr><td align='right'>Administrators</td> <td align='center'><input type='checkbox' name='group[1][]' value='1'></td> <td align='center'><input type='checkbox' name='group[1][]' value='1'</td> <td align='center'><input type='checkbox' name='group[1][]' value='1'></td> <td align='center'><input type='checkbox' name='group[1][]' value='1'></td> <td align='center'><input type='checkbox' name='group[1][]' value='1'></td> <td align='center'><input type='checkbox' name='group[1][]'' value='1'></td> <td align='center'><input type='checkbox' name='allNone' value=''></td> <tr><tr><td align='right'>Developers</td> <td align='center'><input type='checkbox' name='group[2][]' value='1'></td> <td align='center'><input type='checkbox' name='group[2][]' value='1'</td> <td align='center'><input type='checkbox' name='group[2][]' value='1'></td> <td align='center'><input type='checkbox' name='group[2][]' value='1'></td> <td align='center'><input type='checkbox' name='group[2][]' value='1'></td> <td align='center'><input type='checkbox' name='group[2][]'' value='1'></td> <td align='center'><input type='checkbox' name='allNone' value=''></td> <tr><tr></table> When I print the array with PHP for the group input, this is what returns when I have all fields checked for the Admins group but only Read and Comment checked for developers. Array( [1] => Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 1 ) [2] => Array ( [0] => 1 [1] => 1 )) If the script was working the way I want it to, it should look like this. Array( [1] => Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 1 ) [2] => Array ( [0] => 1 [1] => 0 [2] => 0 [3] => 0 [4] => 1 [5] => 0 )) I'm not getting any errors in firebug's error console either before or after i submit the form. Link to comment Share on other sites More sharing options...
jeffman Posted March 13, 2010 Share Posted March 13, 2010 If you'd said what you wanted to begin with . . .A form will not post checkbox info if the checkbox is not checked. It's that simple. The data-collection logic for checkboxes has nothing to do with the value property. Only the checked property. It's a different kind of logic than for text fields, say.One solution might be to associate every checkbox with a hidden input. In your loop, instead of toggling the value of a checkbox, set the value of the corresponding hidden input. The names or IDs of the corresponding inputs could be related somehow. So (for example) adding a "1" to the ID of a checkbox gives you the ID of the correct hidden input. That would keep your checkBoxes function from growing very much. Link to comment Share on other sites More sharing options...
hybrid kill3r Posted March 13, 2010 Author Share Posted March 13, 2010 Ok, so if this is my new form, how would I loop through the checkboxes that are checked and set the corresponding hidden fields to 1? <table align='center' cellspacing='1' cellpadding='3' border='0'> <tr> <td align='right' width='30%'>Group Name</td> <td align='center' width='10%'>Read</td> <td align='center' width='10%'>Create</td> <td align='center' width='10%'>Modify</td> <td align='center' width='10%'>Attach</td> <td align='center' width='10%'>Comment</td> <td align='center' width='10%'>Delete</td> <td align='center' width='10%'>All/None</td> </tr><tr><td align='right'>Administrators</td> <td align='center'><input type='checkbox' name='group[1][]' value='1'></td> <input type='hidden' name='hidden[1][]' value='0'> <td align='center'><input type='checkbox' name='group[1][]' value='1'</td> <input type='hidden' name='hidden[1][]' value='0'> <td align='center'><input type='checkbox' name='group[1][]' value='1'></td> <input type='hidden' name='hidden[1][]' value='0'> <td align='center'><input type='checkbox' name='group[1][]' value='1'></td> <input type='hidden' name='hidden[1][]' value='0'> <td align='center'><input type='checkbox' name='group[1][]' value='1'></td> <input type='hidden' name='hidden[1][]' value='0'> <td align='center'><input type='checkbox' name='group[1][]'' value='1'></td> <input type='hidden' name='hidden[1][]' value='0'> <td align='center'><input type='checkbox' name='allNone' value=''></td> <tr><tr><td align='right'>Developers</td> <td align='center'><input type='checkbox' name='group[2][]' value='1'></td> <input type='hidden' name='hidden[2][]' value='0'> <td align='center'><input type='checkbox' name='group[2][]' value='1'</td> <input type='hidden' name='hidden[2][]' value='0'> <td align='center'><input type='checkbox' name='group[2][]' value='1'></td> <input type='hidden' name='hidden[2][]' value='0'> <td align='center'><input type='checkbox' name='group[2][]' value='1'></td> <input type='hidden' name='hidden[2][]' value='0'> <td align='center'><input type='checkbox' name='group[2][]' value='1'></td> <input type='hidden' name='hidden[2][]' value='0'> <td align='center'><input type='checkbox' name='group[2][]'' value='1'></td> <input type='hidden' name='hidden[2][]' value='0'> <td align='center'><input type='checkbox' name='allNone' value=''></td> <tr><tr></table> Link to comment Share on other sites More sharing options...
jeffman Posted March 13, 2010 Share Posted March 13, 2010 The names or IDs of the corresponding inputs could be related somehow. So (for example) adding a "1" to the ID of a checkbox gives you the ID of the correct hidden input.What I had in mind was more like this:<input type='checkbox' name='group[1][]' value='1' id='CBa'><input type='hidden' name='hidden[1][]' value='0' id='CBa1'><input type='checkbox' name='group[1][]'' value='1' id='CBb'><input type='hidden' name='hidden[1][]' value='0' id='CBb1'> Each pair is now uniquely identified and matched to each other. If we don't do that, we could end up with a mess. The code below works with elements defined like that. To make it work, you'll have to follow that pattern when you assign id's to the rest of your inputs. Support for getElementsByName is flawed, so I'm not going to use it. You can do what you want. Let's pretend your table has the id "myTable". function checkBoxes(){ var id; var inputs = document.getElementById("myTable").getElementsByTagName("input"); var len = inputs.length; for(i = 0; i < len; ++i){ if(inputs[i].type.toLowerCase() == "checkbox") { id = inputs[i].id + "1"; if (inputs[i].checked == true){ document.getElementById(id).value = "1"; } else { document.getElementById(id).value = "0"; } } }} Link to comment Share on other sites More sharing options...
hybrid kill3r Posted March 14, 2010 Author Share Posted March 14, 2010 OMG, thank you. I have been trying to figure out this problem for weeks. Thanks so much. Link to comment Share on other sites More sharing options...
hybrid kill3r Posted March 17, 2010 Author Share Posted March 17, 2010 Hate to be the barer of bad news, but it only works for the first row. It's because the first row isn't necessarily always 0. Actually it's never zero because the first array is based on an auto-incremented integer that starts at 1. Link to comment Share on other sites More sharing options...
jeffman Posted March 17, 2010 Share Posted March 17, 2010 Sorry, I'm not sure I follow. Does the problem have to do with assigning values, or is it a problem with your naming pattern? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 17, 2010 Share Posted March 17, 2010 Why are you interested in submitting values for checkboxes which were not checked, anyway? PHP can already determine whether or not a box was checked. Wouldn't it be easier to do all of the logic in PHP? Link to comment Share on other sites More sharing options...
ShadowMage Posted March 17, 2010 Share Posted March 17, 2010 Why are you interested in submitting values for checkboxes which were not checked, anyway? PHP can already determine whether or not a box was checked. Wouldn't it be easier to do all of the logic in PHP?For example:if (isset($_POST['group'][2][1])) {//do something} else {//do something else} Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.