Checkbox processing -- JavaScript and PHP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jason,

here is JS code to check all the checkboxes in a form,
this code will prevent errors and should work fin in
all browsers:
-----
function CheckAll(obj, form)
{
	var count = form.elements.length ;
	
	for (i=0; i<count; i++)
	{
		if ( form.elements[i].type == 'checkbox')
		{
			if ( obj.checked)
			{
				form.elements[i].checked = true ;
                                // Put more code here
			}
			else 
			{
				form.elements[i].checked = false ;
                                // Put more code here
			}
		}
	}
}
-----
<form action="#" method="post" id="myForm" >
<input type="checkbox" name="checkbox4"
value="checkbox" name="SelectAll" id="SelectAll"
onClick="CheckAll(this, myForm)" />
</form>
-----
Just pass in the ID of the checbox and of the form,
then the function does the rest! 

If you need server-side processing in PHP, you can
take this function with very small change to make it
work, just append the brackets `[]' chars to the name
attribute of the checkbox, and PHP will automaticlly
make it an array, then you process as needed. Good
luck
-----
<input type="checkbox" name="checkbox4"
value="checkbox" name="SelectAll[]" id="SelectAll"
onClick="CheckAll(this, myForm)" />
-----
<?php

$arrAllCheckboxes = $_POST['SelectAll'] ;
for ($i=0; $i<count($arrAllCheckboxes); $i++)
{
  if ( $arrAllCheckboxes[$i] == 'checkbox' )
  {
    // BOX IS CHECKED
  }
  else
  {
    // BOX NOT CHECKED
  }
}
?>
----
Greg



> On Sat, 18 Sep 2004 13:05:07 -0000, Jason
> <jason@haffey.com> wrote:
> > I have been racking my brain to figure out how
> they do this...
> > 
> > If you go to just about any web based email, you
> will have Check
> > Boxes down the side next to each email.  There is
> the one check box
> > at the top that if you click, all the rest "check
> up".  That part
> > already did (with javascript) but then when I try
> to do a deleted,
> > mark read, or etc. I cant figure out how to run a
> loop to check each
> > checkbox.



	
		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
http://promotions.yahoo.com/new_mail 


------------------------ Yahoo! Groups Sponsor --------------------~--> 
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~-> 

PHP Data object relational mapping generator - http://www.meta-language.net/ 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-objects/

<*> To unsubscribe from this group, send an email to:
    php-objects-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux