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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP, Javascript, checkboxes, check all function

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?
 
Without tearing through your code here is a really simple example of what you want to do with Javascript. I'm sure you'll be able to adapt it for your uses:

Code:
<html>
<head>
<script language=&quot;Javascript&quot;>
function check(form)
{
    for (var x =0; x < document.test.elements.length; x++)
    {
      if (document.test.elements[x].type.indexOf('check') != -1)
	  { 
	    document.test.elements[x].checked = document.test.markall.checked;
	  }
	}  	 
}
</script>
</head>
<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<form name=&quot;test&quot; action=&quot;checkall.html&quot; method=&quot;post&quot;>
check All   <input type=&quot;checkbox&quot; name=&quot;markall&quot; onClick=&quot;check()&quot;><br>
Check one   <input type=&quot;checkbox&quot; name=&quot;one&quot;>
Check two   <input type=&quot;checkbox&quot; name=&quot;two&quot;>
Check three   <input type=&quot;checkbox&quot; name=&quot;three&quot;>
Check four   <input type=&quot;checkbox&quot; name=&quot;four&quot;><br>
</form>
</body>
</html>

Just cut-n-paste it and try it out, then you can take it apart and make it work for you. It is pretty easy, it will work with all checkboxes regardless of there names as long as you have the name of the checkbox that calls it. Even that could be changed to make it easier but this was quicker... :) let me know if you need any help adapting it.

Hope it helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top