Forum Moderators: coopster

Message Too Old, No Replies

check box

select all boxes & getting data

         

d40sithui

6:01 pm on Jul 30, 2007 (gmt 0)

10+ Year Member



Hi again,
This is what i have. I'm trying to retrieve all the posted check box data without naming the checkboxes as "list[]" becuase this'll screw up the js function to select all and deselect all. any ideas?

<SCRIPT LANGUAGE="JavaScript">
function checkAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = false ;
}
</script>

<form name="myform" action="index.php" method="post">
<b>Your Favorite Scripts & Languages</b><br>
<input type="checkbox" name="list" value="1">Java<br>
<input type="checkbox" name="list" value="2">Javascript<br>
<input type="checkbox" name="list" value="3">Active Server Pages<br>
<input type="checkbox" name="list" value="4">HTML<br>
<input type="checkbox" name="list" value="6">SQL<br>
<input type="checkbox" name="list" value="7">PHP<br>
<input type="checkbox" name="list" value="8">CSS<br>
<input type="checkbox" name="list" value="9">AJAX<br>

<input type="button" name="CheckAll" value="Check All"
onClick="checkAll(document.myform.list)">

<input type="button" name="UnCheckAll" value="Uncheck All"
onClick="uncheckAll(document.myform.list)">
<br>

<input type="submit" value="Submit">
</form>

<?

$list = $_POST['list'];

print_r($list);

?>

joelgreen

6:13 pm on Jul 30, 2007 (gmt 0)

10+ Year Member



I would think something like this, have not tested though

SCRIPT LANGUAGE="JavaScript">
function checkAll()
{
for (i = 0; i < 9; i++)
document.getElementById("list[" + i + "]").checked = true ;
}

function uncheckAll()
{
for (i = 0; i < 9; i++)
document.getElementById("list[" + i + "]").checked = false ;
}
</script>

<form name="myform" action="index.php" method="post">
<b>Your Favorite Scripts & Languages</b><br>
<input type="checkbox" id="list[1]" value="1">Java<br>
<input type="checkbox" id="list[2]" value="2">Javascript<br>
<input type="checkbox" id="list[3]" value="3">Active Server Pages<br>
<input type="checkbox" id="list[4]" value="4">HTML<br>
<input type="checkbox" id="list[5]" value="6">SQL<br>
<input type="checkbox" id="list[6]" value="7">PHP<br>
<input type="checkbox" id="list[7]" value="8">CSS<br>
<input type="checkbox" id="list[8]" value="9">AJAX<br>

<input type="button" name="CheckAll" value="Check All"
onClick="checkAll()">

<input type="button" name="UnCheckAll" value="Uncheck All"
onClick="uncheckAll()">
<br>

<input type="submit" value="Submit">
</form>

<?

$list = $_POST['list'];

print_r($list);

?>

d40sithui

8:32 pm on Jul 30, 2007 (gmt 0)

10+ Year Member



so your script didnt work =/ thanks anyway. from that script however, i was able to form one that does work.
The only downside is that he js might need to be dynamically generated depending on your form. in mine it will.

so here's the revised on that works. feel free to edit it to your liking

<SCRIPT LANGUAGE="JavaScript">
function checkAll(){
for(k=1; k <= 9; k++){
var x=document.getElementsByName("list["+k+"]");

x.checked=true; //does not work
//x[0].checked=true; //works but gives js error

for(i = 0; i < x.length; x++){

x[i].checked=true;
}
}
}
function uncheckAll(){
for(k=1; k <= 9; k++){
var x=document.getElementsByName("list["+k+"]");

x.checked=true; //does not work
//x[0].checked=true; //works but gives js error

for(i = 0; i < x.length; x++){

x[i].checked=false;
}
}
}

</script>

<form name="myform" action="index.php" method="post">
<b>Your Favorite Scripts & Languages</b><br>
<input type="checkbox" name="list[1]" value="1">Java<br>
<input type="checkbox" name="list[2]" value="2">Javascript<br>
<input type="checkbox" name="list[3]" value="3">Active Server Pages<br>
<input type="checkbox" name="list[4]" value="4">HTML<br>
<input type="checkbox" name="list[5]" value="5">SQL<br>
<input type="checkbox" name="list[6]" value="6">PHP<br>
<input type="checkbox" name="list[7]" value="7">CSS<br>
<input type="checkbox" name="list[8]" value="8">AJAX<br>

<input type="button" name="CheckAll" value="Check All" onClick="checkAll();">
<input type="button" name="UnCheckAll" value="Uncheck All" onClick="uncheckAll();">
<br>

<input type="submit" value="Submit">
</form>

<?

$list = $_POST['list'];
print_r($list);

?>