Archived

This topic is now archived and is closed to further replies.

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

This topic is 5510 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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-

Share this post


Link to post
Share on other sites
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 javascript 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 javascript function, passing either true or false as a parameter. Now whenever one of the buttons is pressed it will execute the function with the desired parameter.

Share this post


Link to post
Share on other sites