Forum Moderators: coopster
I have a program which deals with multiple departments and multiple employees.
So I have a checkbox for each departments and also there is a checkbox for each employees. If someone checks one department I want to check all the employees in that departments. Like wise for other departments also. I have used checkbox array but couldn't solve the problem through javascript. Please someone help me out with this.
This is the sample code
<input type=checkbox name='department1[]' value='someID'>
<input type=checkbox name='employees[]' value='someID'>
Thanks
<form action="" name="myform">
dep 1<input type="checkbox" name="department[]" value="1" onclick="checkall(this)"><br>
emp 1<input type="checkbox" name="dep1_employee[]" value="1"><br>
emp 2<input type="checkbox" name="dep1_employee[]" value="2"><br>
emp 3<input type="checkbox" name="dep1_employee[]" value="3"><br>
<br>
dep 2<input type="checkbox" name="department[]" value="2" onclick="checkall(this)"><br>
emp 4<input type="checkbox" name="dep2_employee[]" value="4"><br>
emp 5<input type="checkbox" name="dep2_employee[]" value="5">
</form>
Then simply loop through the relevant array of checkboxes and check or uncheck them:
<script type="text/javascript">
function checkall(dep) {
var empls = document.myform['dep' + dep.value + '_employee[]'];
for (var i = 0; i < empls.length; i++) {
empls[i].checked = dep.checked? true : false;
}
}
</script>
But one more problem happened in the PHP. When I submitted to the PHP program I am not able to get the checkbox values properly according to the departments selected. If it is not selected also the employees values is passing.
Sorry if this question has no relevance. Can you please help me with the code of how to retrive the checkbox valeus in a proper manner..
Thanks once again.