...

View Full Version : Javascript problems and PHP



thepip3r
09-22-2005, 05:34 PM
I've got a form where it's a bunch of checkboxes, each name of the checkbox is "bullets[]" so when the form is submitted, all of the selected checkboxes are in the $_POST['bullets'] array. My problem is that the way I have it set up in my loop, the name "bullets[]" makes my SelectAll and SelectNone Javascript functions not work any longer. JS error: "'length' is null or not an object.' Has anyone else run into this?

Javascript in page head:

<script>
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>

PHP/HMTL Loop for Form Checkboxes and JS Func Buttons:

<input type=button name=\"CheckAll\" value=\"Check All\" onClick=\"checkAll(document.refineForm.bullets)\">
<input type=button name=\"UnCheckAll\" value=\"Uncheck All\" onClick=\"uncheckAll(document.refineForm.bullets)\">

<td rowspan=4 valign=\"top\"><input class=\"norm\" type=\"checkbox\" name=\"bullets[]\" value=\"$jobID\" checked></td>

thepip3r
09-22-2005, 05:48 PM
NM... this worked great.

Fix (http://www.ilovejackdaniels.com/javascript/select-all-javascript-for-forms-posting-to-an-array/)


<html>
<head>
<title>Checkbox Fun</title>
<script type="text/javascript"><!--

var formblock;
var forminputs;

function prepare() {
formblock= document.getElementById('form_id');
forminputs = formblock.getElementsByTagName('input');
}

function select_all(name, value) {
for (i = 0; i < forminputs.length; i++) {
// regex here to check name attribute
var regex = new RegExp(name, "i");
if (regex.test(forminputs[i].getAttribute('name'))) {
if (value == '1') {
forminputs[i].checked = true;
} else {
forminputs[i].checked = false;
}
}
}
}

if (window.addEventListener) {
window.addEventListener("load", prepare, false);
} else if (window.attachEvent) {
window.attachEvent("onload", prepare)
} else if (document.getElementById) {
window.onload = prepare;
}

//--></script>
</head>

<body>

<form id="form_id" name="myform" method="get" action="search.php">

<a href="#" onClick="select_all('area', '1');">Check All Fruit</a> | <a href="#" onClick="select_all('area', '0');">Uncheck All
Fruit</a><br><br>

<input type="checkbox" name="area[]" value="1" />Apples<br />
<input type="checkbox" name="area[]" value="2" />Bananas<br />
<input type="checkbox" name="area[]" value="3" />Chickens<br />
<input type="checkbox" name="area[]" value="4" />Stoats

<br><br><a href="#" onClick="select_all('location', '1');">Check All Locations</a> | <a href="#" onClick="select_all('location',
'0');">Uncheck All Locations</a><br><br>

<input type="checkbox" name="location[]" value="1" />Brighton<br />
<input type="checkbox" name="location[]" value="2" />Hove<br />

</form>

</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum