PHP/Javascript Checkboxes

Links for php scripts

Moderators: macek, egami, gesf

chellert
New php-forum User
New php-forum User
Posts: 10
Joined: Tue Apr 20, 2010 8:54 am

PHP/Javascript Checkboxes

Postby chellert » Tue Mar 13, 2012 12:07 pm

Hello

I am trying to use javascript and PHP together to be able to select all checkboxes in an array within a certain group of checkboxes.

My code in the header is as follows:

<script>
function select(a) {
var theForm = document.myForm;
for (i=0; i<theForm.elements.length; i++) {
if (theForm.elements[i].name=='mycheckbox[]')
theForm.elements[i].checked = a;
}
}
</script>


The site lists properties, you click on a property name, it will open up (on the same page) and list all users allowed to view that property.. You can click on the name again and the list of users will disappear. I want to be able to select All checkboxes for this group without affecting any other properties on the page. I will have a list of 8 properties with approximately 30 users underneath each property.

Within a while loop I place the property name, with a hide funcation for all of the users underneath. The loop then places all of the users from a database under the name, the following code is set up for each checkbox:
<form name="myForm">
<input type='checkbox' name='mycheckbox[]' value='{$propf['us_id']}'>
</form>
<a href='javascript:select(1)'>Check All</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href='javascript:select(0)'>Uncheck All</a>

I have used an ID number for each of the group checkboxes and I still receive the following error when I click on 'Check All':
Message: 'elements.length' is null or not an object
Line: 12
Char: 12
Code: 0

Can anyone help me figure this out?

LarryMade
New php-forum User
New php-forum User
Posts: 41
Joined: Wed Mar 14, 2012 5:21 am

Re: PHP/Javascript Checkboxes

Postby LarryMade » Thu Mar 15, 2012 6:52 am

Here is the check all code I use - maybe it will help you build what you need:

This is the function I use to display the "check all" button (a checkmark icon) and define what nameset it is toggling.

Code: Select all

function checkAllButton($list)
{
$string = '<input id="checkall" type="checkbox" name=""
onClick="checkAll(this,\''.$list.'\')"><label for="checkall"><img src="'.WANDA_ICON.'check1.png"></label>';
    return $string;
}


if you have checkbox elements like:
<type="checkbox" name="checklist[]" value="somevalue">

you would use:
echo checkAllButton('checklist[]');
to display a checkall for those checkboxes.

and here is the javascript to do the checkall:

Code: Select all

<script = "javascript" >
/*<![CDATA[*/

// the name array will not be generated if the element name is illegal so we use a loop
function checkAll(obj,nme){
 var eles=obj.form.elements;
 for (var zxc0=0;zxc0<eles.length;zxc0++){
  if (eles[zxc0].name&&eles[zxc0].name==nme) eles[zxc0].checked=obj.checked;
 }
}
/*]]>*/
</script>

chellert
New php-forum User
New php-forum User
Posts: 10
Joined: Tue Apr 20, 2010 8:54 am

Re: PHP/Javascript Checkboxes

Postby chellert » Tue Mar 20, 2012 6:09 am

I think my problem is the name of the checkbox. for instance:

Property A
checkbox 1
checkbox 2
checkbox 3
Property B
checkbox 1
checkbox 2
Property C
checkbox 1
checkbox 2

would this cause any problems?

LarryMade
New php-forum User
New php-forum User
Posts: 41
Joined: Wed Mar 14, 2012 5:21 am

Re: PHP/Javascript Checkboxes

Postby LarryMade » Tue Mar 20, 2012 6:54 am

They are all different names so you will need to craft some javascript like the one I provided to work with varying names (Javascript isn't my strong point) I found what I used with google, maybe you should give it a go there... or in the Javascript forum. :D


Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 7 guests