Jump to content

Help with checkbox that checks off all checkboxes


damion

Recommended Posts

Hi,

 

 

I hope someone can help me with this. I need to add a checkbox that will be a 'check off all' applied to other checkboxes. Below is my code, and the checkboxes that I want to have automatically checked off are the 'extensions' checkboxes that you will see at the bottom of the script.

	
   $extStr="";
   $new_extensions = getExtensions();
   $cc=1;
   for($i=0;$i<count($new_extensions);$i++){ 
    	$extStr .= "<input type=\"checkbox\" name=\"extensions[]\"  value=\"".$new_extensions[$i]."\" ";
	if(is_array($extensions)){ if(in_array($extensions,$new_extensions[$i])){ $extStr .= " checked "; } } else if($extensions==$new_extensions[$i]){   $extStr .= " checked "; }
	$extStr .= "/>.".$new_extensions[$i]; 
	$cc++;
	if($cc==9){ $extStr.="<br />";$cc=1; }
   }
   

   $upload_dirs="";
   $query="SELECT * FROM folders ORDER BY name ASC";
   $result=mysql_query($query) or die("error getting folders from database");
   while($rr=mysql_fetch_assoc($result)){
   		$upload_dirs .= "<option value='".$rr["name"]."'>".$rr["name"]."</option>";
   }
   
    //new in version 1.4, user status
   $user_stat="";
   		$user_stat .= "<option value='2' ".($active=="2"?"selected":"").">Not Active</option>";
		$user_stat .= "<option value='1' ".($active=="1"?"selected":"").">Active</option>";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Welcome <?php echo $_SESSION["username"]?>!</title>
<link rel="stylesheet" href="css/filemanager.css" type="text/css" />
</head>
<body>
<div id="content">
  
  <h1>File Management</h1>
  

<?php require("includes/menu.php");?> 

<div class="content_block">
  <h2>Add New User</h2>
  <strong><?php echo $msg; ?></strong>
  <form action="users.php" enctype="multipart/form-data" method="post" name="ff1">
  <table width="784" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="152" height="25" align="right">Username:</td>
    <td width="632" height="23">
      <input type="text" name="username" id="username" value="<?php echo $username?>" />
    </td>
  </tr>
    <tr>
    <td height="25" align="right">Status:</td>
    <td height="23">
    <select name="active" id="active">
    <option value="">Please Select</option>
    <?php echo $user_stat; ?>
    </select>
    </td>
  </tr>
  <tr>
    <td height="25" align="right">Password:</td>
    <td height="23"><input type="text" name="password" id="password" /></td>
  </tr>
  <tr>
    <td height="25" align="right">Email:</td>
    <td height="23"><input type="text" name="email" id="email" value="<?php echo $email?>" /></td>
  </tr>
  <tr>
    <td height="25" align="right" valign="top">Access Level:</td>
    <td height="23">
      <input type="checkbox" name="accesslevel2[]" value="g" <?php if(is_array($accesslevel12) && in_array($accesslevel2,"g")){ echo "checked"; }?> /> Can upload files<br />
      <input type="checkbox" name="accesslevel2[]" value="i" <?php if(is_array($accesslevel12) && in_array($accesslevel2,"i")){ echo "checked"; }?> /> Can view everyones files<br />
      <input type="checkbox" name="accesslevel2[]" value="h" <?php if(is_array($accesslevel12) && in_array($accesslevel2,"h")){ echo "checked"; }?> /> Can delete his/her files<br />
      <input type="checkbox" name="accesslevel2[]" value="j" <?php if(is_array($accesslevel12) && in_array($accesslevel2,"j")){ echo "checked"; }?> /> Can delete any file.<br />
      <input type="checkbox" name="accesslevel2[]" value="k" <?php if(is_array($accesslevel12) && in_array($accesslevel2,"k")){ echo "checked"; }?> /> Can edit his files.<br />
      <input type="checkbox" name="accesslevel2[]" value="l" <?php if(is_array($accesslevel12) && in_array($accesslevel2,"l")){ echo "checked"; }?> /> Can edit all files.<br />
      <input type="checkbox" name="accesslevel2[]" value="m" <?php if(is_array($accesslevel12) && in_array($accesslevel2,"m")){ echo "checked"; }?> /> Can share any files.<br />
    </td>
  </tr>
  <tr>
    <td height="25" align="right" valign="top">Extensions:</td>
    <td height="23">
    <?php echo $extStr;?></td>
  </tr>
  <tr>

Thank you to anyone who can help me on this.

Link to comment
Share on other sites

Typically when dealing with checkboxes, if you want 1 checkbox to allow all checkboxes checked it would be done with javascript.

 

If you want PHP to check off all checkboxes within a loop you would add

 checked="checked"

to the html code of the checkbox themselves.

Link to comment
Share on other sites

You should include jQuery http://jquery.com/ and give all the checkboxes you want auto-checked a class="auto-check", and the "check all" checkbox an onclick="$('.auto-check').each(function(index) { this.attr('checked','checked');});" event.

 

Some references: http://www.aliaspooryorik.com/blog/index.cfm/e/posts.details/post/toggle-multiple-checkboxes-with-jquery-7 , http://api.jquery.com/each/

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.