Page 1 of 1

Check all box

Posted: Mon May 19, 2003 4:30 pm
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";
}

Posted: Mon May 19, 2003 4:46 pm
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>

Posted: Mon May 19, 2003 7:06 pm
by McGruff
Or an extra checkbox, name="all", value="123456" - substr() to get individual values later.

It works but...

Posted: Tue May 20, 2003 1:28 pm
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

Posted: Tue May 20, 2003 7:57 pm
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.

Posted: Wed May 21, 2003 2:05 am
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...