Check all box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

Check all box

Post by mccommunity »

I am creating a table with values, each value has a check box next to it. I would like to add a box at the top that says check all so when it is checked all the boxes get checked. I tried a javascript to do this. When I did that it would check all of the boxes but did not throw these into my array (attendee_id[] ) instead it just passed in the last value(record) instead of all of them. When I manually check them all it works fine. Can anyone help me with this? I am stuck 8O .... Thank you so much for your time.
mod_edit: php tags added

Code: Select all

echo "<table class=listing width=100%>\n";
  echo "<tr>\n";
// this is the box I created to select all from
  echo "  <th>Selected<br><input name=select_all type="checkbox" </th>\n";
  echo "  <th>Username</th>\n";
  echo "  <th>Password</th>\n";
  echo "  <th>E-mail</td>\n";
  if ($credit)
  {
    echo "  <th>Credit Limit</td>\n";
  }
  echo "  <th>View/Modify</th>\n";
  echo "</tr>\n";

  $high_row = false;
  foreach ($attendee_ids as $id)
  {
    $info = $attendee_session->GetAttendeeInfo($id, 0);

    if ($high_row)
    {
      echo "<tr class=high>\n";
    }
    else
    {
      echo "<tr>\n";
    }

    $high_row = ! $high_row;

    echo "<td><input type=checkbox name=attendee_id[] value=$info->id ></td>\n";
    echo "<td>$info->username</td>\n";
    echo "<td>$info->password</td>\n";
    echo "<td>$info->email</td>\n";

    if ($credit)
    {
      echo "<td>$info->credit_limit</td>\n";
    }

    echo "<td><a href=$module_url&action=modify&attendee_id=$info->id>View/Modify</a></td>\n";

    echo "</tr>\n";
  }

  echo "</table>\n";
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

this works fine for me

Code: Select all

<html>
	<head>
		<script type="text/javascript">
			function checkAll()
			{
				oColl = document.getElementsByName("chk[]");
				for (i=0; i!=oColl.length; i++)
					oColl[i].checked = "true";
			}
		</script>
	</head>
	<body>
		<pre><?php print_r($_POST); ?></pre>
		<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
			<input type="button" onClick="javascript:checkAll()" value="check all" /> <br />
			<input type="checkbox" name="chk[]" value="1" />
			<input type="checkbox" name="chk[]" value="2" />
			<input type="checkbox" name="chk[]" value="3" />
			<input type="checkbox" name="chk[]" value="4" />
			<input type="checkbox" name="chk[]" value="5" />
			<input type="checkbox" name="chk[]" value="6" />
			<br />
			<input type="submit" />
		</form>
	</body>
</html>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Or an extra checkbox, name="all", value="123456" - substr() to get individual values later.
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

It works but...

Post by mccommunity »

Hey thanks for the replies that works. There is only one small problem. If I click the check all box then uncheck some or all of them, the uncheck box stays checked. Is there a way that if all the boxes are not checked it will take the check out of the check all box? Thanks.


Mark
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

That would have to be javascript. I would probably just assume that if the user had checked an "all" box it's up to them to uncheck it again if they want to select individual options - but I guess you never can predict exactly how people will interact with stuff. Maybe an explanatory note beside the "all" box could help.
ckuipers
Forum Commoner
Posts: 61
Joined: Mon Mar 24, 2003 6:10 am

Post by ckuipers »

McGruff wrote:That would have to be javascript. I would probably just assume that if the user had checked an "all" box it's up to them to uncheck it again if they want to select individual options - but I guess you never can predict exactly how people will interact with stuff. Maybe an explanatory note beside the "all" box could help.
Exactly, in some cases, like me, people use the 'select all' to then uncheck one instead of checking them one by one. It's a time-saver...
Post Reply