Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP, Checkboxes, and Javascript checkall()

Status
Not open for further replies.

ryebread

Programmer
Aug 13, 2001
29
US
My problem probally is a common one, I'm sure someone's experience will help me out here!

I am using check boxes in a form to return an array of values to a php script. I need to have a check all checkbox that checks every other checkbox. Seems like i need to use javascript to do this.

My problem comes up when i need to place the [] in the field name (for php to recognize an array). The javascript works when i ommit the [], but then php keeps overwriting the field insted of seeing it as an array.

I call this function like this: area_checkbox($db,"area[]");

function area_checkbox($connect,$cname){

$foo=list_areas($connect);

echo &quot;<SCRIPT LANGUAGE='JavaScript'>

function dochange(){
foo = document.asearch.elements['$cname'];
box=ducument.asearch.abox;

if(box.checked==true)
checkAll(foo);
else
uncheckAll(foo);
}

function checkAll(foo){

for (i = 0; i < foo.length; i++){
foo.checked = true ;
}
}

function uncheckAll(foo){
for (i = 0; i < foo.length; i++){
foo.checked = false ;
}
}
</script>&quot;;



print(&quot;<table border=1 cellpadding=5>&quot;);
for($cntr=0;$cntr<count($foo);$cntr++){

if(($cntr+1)%3==1)
echo &quot;<tr>&quot;;

list($code,$name)=$foo[$cntr];
print(&quot;<td width=120><input type='checkbox' value='$code' name='$cname'>$name</td>&quot;);

if(($cntr+1)%3==0)
echo &quot;</tr>&quot;;
}


if((count($foo)+1)%3<>0)
echo &quot;</tr>&quot;;

echo &quot;<tr><td width=120 colspan=3><input type='checkbox' name='abox' onClick='dochange();'>Check All</td></tr></table>&quot;;
}

Any Ideas?
 
My typo happen to be the only problem!

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top