Forum Moderators: coopster

Message Too Old, No Replies

Checkbox array help

         

babloo

8:19 am on Jul 26, 2004 (gmt 0)

10+ Year Member



Hello,

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

RonPK

10:29 am on Jul 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



IMHO this really should be done client side, with JavaScript. You will need a more suitable naming structure, something like


<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>

babloo

10:44 am on Jul 26, 2004 (gmt 0)

10+ Year Member



Thanks a lot.This really helped me.

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.

RonPK

1:22 pm on Jul 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It always helps me a lot to see what arrives at the server. Use this bit of PHP code:

echo '<pre>';
print_r($_REQUEST);
echo '</pre>';

Now you should have a clear view of all the arrays and strings that were submitted. I'll leave the parsing to you, that's the easy part ;).