...

View Full Version : Looping through all form fields hangs the system



victaru
07-20-2009, 10:37 AM
Hi all,

I'm currently working on a PHP project, in which i have a search form for users to set search criteria and displays the result. for each result row i have created a checkbox for the user to select that row such that they will be able to identify which rows to export out.

now, as this could be a potentially generate a huge list of results, individually selecting each checkbox can be a pain. so i created a "toggle" checkbox which will toggle the checkboxes "on" or "off" using a javascript function.

the HTML code for the "toggle"


<INPUT TYPE='checkbox' NAME='AutoCheck' VALUE='Autocheck-uncheck All Boxes' onClick='modify_boxes()'>

and the code for each of the individual checkboxes


<input type='checkbox' name='expID[]' value='" . $info['ID'] . "'>

the JS function



function modify_boxes(){
for ( i=0 ; i <theForm.elements.length ; i++ ){
if (theForm.elements[i].name=='expID[]'){
if (theForm.AutoCheck.checked == true) {
theForm.elements[i].checked = true;
}else{
theForm.elements[i].checked = false;
}
}
}
}


now, as this could be a potentially generate a huge list of results, looping through all the form elements looping through all the elements in the form seems inefficient (i have many other elements besides this checkbox), is there another way to achieve the same result (i.e. autocheck/uncheck) in a more effective manner?

Thanks in advance!

vwphillips
07-20-2009, 11:05 AM
<!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>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/
function modify_boxes(obj){
var cbs=document.getElementsByName('expID[]');
for (var i=0 ; i <cbs.length; i++ ){
cbs[i].checked=obj.checked;
}
}
/*]]>*/
</script></head>

<body>
<INPUT TYPE='checkbox' NAME='AutoCheck' VALUE='Autocheck-uncheck All Boxes' onClick='modify_boxes(this)'>
<input type='checkbox' name='expID[]' value=''>

</body>

</html>

victaru
07-20-2009, 11:23 AM
good lord, thanks!

although it still took a while, but it manages to check all 2000 test records i had :)

Thanks!

randomuser773
07-20-2009, 12:11 PM
although it still took a while, but it manages to check all 2000 test records i had This should make it slightly faster:

function modify_boxes(obj){
var cbs=document.getElementsByName('expID[]');
for (var i=0, len=cbs.length ; i < len; i++ ){
cbs[i].checked=obj.checked;
}
}

Shannon Blonk
07-21-2009, 12:51 AM
And a wee bit faster still
function modify_boxes(obj){
var cbs=document.getElementsByName('expID[]'), state=obj.checked;
for (var i=0, len=cbs.length ; i < len; i++ ){
cbs[i].checked=state;
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum