0

I'm trying to implement a page with a choice of user's preferences in an HTML form where if the checkbox ALL is selected then all sub-checkbox base1, base2 and base3 are checked automatically, and if any of sub-checkboxes is un-selected then the checkbox ALL must be unchecked. I used a javascript function which works but when I submit the form only the last variable in the array of checkboxes is sent.

<SCRIPT LANGUAGE="JavaScript">       
function checkChoice(field, i) {

if (i == 0) { // "All" checkbox selected.
if(field[0].checked==true) {
for (i = 1; i < field.length; i++)
field[i].checked = true;
   }
}
else  {  
if (field[i].checked == false) {
field[0].checked = false;
      }
   }
}

<form name="form" method = "POST" action="preferences.php">

<input type=checkbox name=classes1 value="allbases" onclick="checkChoice(document.form.classes1, 0)">All bases
</td><td>
<input type=checkbox name=classes1 value="base1" onclick="checkChoice(document.form.classes1, 1)">Base1
<br>
<input type=checkbox name=classes1 value="base2" onclick="checkChoice(document.form.classes1, 2)">Base2
<br>
<input type=checkbox name=classes1 value="base3" onclick="checkChoice(document.form.classes1, 3)">Base3
<input type="submit"  value="Set preferences" >  

If I call the checkboxes'names in "classes1[]" all the values are submited but the javascript function doesn't work anymore. Is there a way of fixing this? Thanks for any help.

2 Answers 2

0

For an alternative of checkChoice: check this SO question and the jsfiddle I presented there.

[edit] concerning your comment: a bit of extra thinking would have brought you to this solution

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, this could work if I had only one set of checkboxes in the form but I have six different sets like the one I posted so I cannot use getElementsbyTagName(input).
@Kooilnc Thank your for your help (I'm not at all familiar with javascript), in the end the combination works for me function checkChoice(field, k) { if (field[k].checked == false) { field[0].checked = false; } } function flipCbs(obj) { var cbs = document.getElementById('theForm').getElementsByTagName('input'), l = cbs.length, rangeid = obj.id.substr(0,2); while (l--){ if (cbs[l].type.match(/checkbox/i) && cbs[l] !== obj && cbs[l].id.substr(0,2) === rangeid) { cbs[l].checked = obj.checked; } } }
@Kooilnc actually, the combination doesn't work as the POST array is always re-written but I'll keep trying.
@Kooilnc I used your function to create another I needed like this and it finally works! function flip(obj) { var commons = document.getElementById('theForm').getElementsByTagName('input'), k = commons.length, rangeid = obj.id.substr(0,2); while (k--){ if (obj.checked==false&&commons[k].type.match(/checkbox/i)&&commons[k].id.substr(0,2) === rangeid&&commons[0].checked == true) { commons[0].checked = obj.checked; } } }
Hi Tomasole, glad I could contribute. Javascript is a nice & powerfull language once you get the hang of it. Happy coding!
0

All of the values actually ARE submitted but PHP will overwrite $_POST['classes1'] each time until you are just left with the last value. If however you add '[]' to your input names then they are added to an array.

Since the latter causes a problem with javascript, can you not just either a) iterate all of the form elements from the form.elements array, or b) give each input a unique id and use document.getElementById() to find it?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.