Check All Button ( Gah, Still Need Help Please =( )

Started by
3 comments, last by Ketay 21 years, 5 months ago
Okay, im my mail script I am wondering the simplest way to check all checkboxes. The script I have is: -------------------------- $select_trimail = mysql_query("select * from trimail where mto = '$playerinfo[username]' AND mnew = '1' order by id DESC"); echo "<form action=mail.php?action=del method=post>"; echo"<table border=1 width=595 bordercolor=black rules=cols cellspacing=2 cellpadding=2><tr><td width=345 bgcolor=black>Subject:<td width=150 bgcolor=black>From:<td width=100 bgcolor=black>Status:"; while($trim = mysql_fetch_array($select_trimail)) { echo"<tr><td> <input type=checkbox name=del[] value=$trim[id]> $trim[mtitle] <td>$trim[mfrom] <td>Unread "; } $select_trimail2 = mysql_query("select * from trimail where mto = '$playerinfo[username]' AND mnew = '0' order by id DESC"); while($trim = mysql_fetch_array($select_trimail2)) { echo"<tr><td> <input type=checkbox name=del[] value=$trim[id]> $trim[mtitle]<td>$trim[mfrom]<td>Read"; } $ct = mysql_fetch_row( mysql_query("SELECT COUNT(id) FROM trimail where mto='$playerinfo[username]'") ); $c = $ct[0]; echo"</table><input type=submit value='Delete Selected Messages'>"; ------------------------- And I need to make a button that you can click that will check all the ""<input type=checkbox name=del[] value=$trim[id]>"" checkboxes. So if you can help please do. Thanks. -Moo- [edited by - Ketay on November 10, 2002 8:35:16 PM] [edited by - Ketay on November 10, 2002 11:38:06 PM] [edited by - Ketay on November 10, 2002 11:49:01 PM]
-Moo-
Advertisement
Could something along these lines work?

  for(i=0; i<document.MyForm.length; i++)  {   item=document.MyForm.elements[i];   if (item.type=='checkbox' && item.name == 'CheckBoxName')	item.checked=true;  }   


Just make sure your form has a name (MyForm in this case), change "CheckBoxName" to whatever name your checkboxes have (or have a function to do a substring comparision, so you can have "CheckBox1", "CheckBox2" ....) and stick the whole thing in a function that gets called by a buttons onClick event.

[edited by - michalson on November 10, 2002 9:10:14 PM]

[edited by - michalson on November 10, 2002 9:10:40 PM]
Okay can you explain that a bit more? Like would it be on.Click=''SOMETHINGHERE'' or something? And what would that something be? And how do I stick it in a function? And also when I put that code inside something it comes out with it saying that I am missing a ";" but I cant figure out which part of it needs it...

-Moo-
-Moo-
Ok, I''ve uploaded an example to here.

This is the source code

  <script language="JavaScript"><!--  function setCheckState(checkState)	{	 for(i=0; i<document.CheckForm.length; i++)  	 {   	  myElement=document.CheckForm.elements[i];   		if (myElement.type==''checkbox'' && myElement.name.substring(0,12) == "AutoCheckBox")	myElement.checked=checkState;  	 }	}//--></script><form name="CheckForm">AutoCheckBox1 <input type=checkbox name="AutoCheckBox1" value="False"><br>AutoCheckBox2 <input type=checkbox name="AutoCheckBox2" value="False"><br>OtherCheckBox <input type=checkbox name="OtherCheckBox" value="False"><br>AutoCheckBox3 <input type=checkbox name="AutoCheckBox3" value="False"><br>AutoCheckBox4 <input type=checkbox name="AutoCheckBox4" value="False"><br> <button type="button" onClick="setCheckState(true)">Check All</button><button type="button" onClick="setCheckState(false)">Uncheck All</button></form>  

In the first part of the source code (inside the SCRIPT tags) we define a &#106avascript function which will use a for loop to iterate through all the items on the form call "CheckForm". Using a conditional if statement it will find those items that are both checkboxes, and that begin with "AutoCheckBox" and proceed to set their checked value to whatever we specify. Then we create the form with normal HTML, being sure to name the form and include two buttons. In both of these buttons we add a property "whenclickedon" and set it to call the &#106avascript function, passing either true or false as a parameter. Now whenever &#111;ne of the buttons is pressed it will execute the function with the desired parameter.
Ooh! Thanks so much! What I was doing wrong was I wasnt naming my form. *Bops himself*. Thanks again XD

-Moo-
-Moo-

This topic is closed to new replies.

Advertisement