PDA

View Full Version : check all boxes problem


tsclan
09-13-2004, 12:26 AM
I have this javascript here

Head


<script language="JavaScript" type="text/javascript">
<!-- Begin
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
return "Uncheck All"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
return "Check All"; }
}
// End -->
</script>


Body


<input type="checkbox" name="position" value="Admin" class="forminput">
User (Admin) Access<br>
<input type="checkbox" name="position" value="Articles" class="forminput">
Articles Access<br>
<input type="checkbox" name="position" value="News" class="forminput">
News Access<br>
<input type=button value="Select All" class="forminput" onclick="this.value=check(this.form.position)">


That works fine but at the moment I need to name the tick boxes Differently so I can save the data induvidually.
(because at the moment echo $_POST['position']; is just showing one value and not all the vaules that were ticked).
But that would mean the tick all box wont work because they wont be named all the same

Any Help would be great Thanks

Willy Duitt
09-13-2004, 01:41 AM
Try this:


<script type="text/javascript">
<!--//
function check(input){
for(var i=0; i<input.form.elements.length; i++){
if(input.form.elements[i].type.match(/checkbox/i)){
if(input.value.match(/^Select/i)){ input.form.elements[i].checked = 1 };
else{ input.form.elements[i].checked = 0 };
}
} input.value = (input.value == 'Select All') ? 'Unselect All' : 'Select All';
}
//-->
</script>
</head>

<body>
<form>
<input type="checkbox" name="Admin" value="Admin">User (Admin) Access<br>
<input type="checkbox" name="Articles" value="Articles">Articles Access<br>
<input type="checkbox" name="News" value="News">News Access<br>
<input type="button" value="Select All" style="width:110px" onclick="check(this)">
</form>


.....Willy

Vladdy
09-13-2004, 02:44 AM
Just a little optimization:


<script type="text/javascript">
function check(input)
{ for(var i=0; i<input.form.elements.length; i++)
if(/checkbox/i.test(input.form.elements[i].type) && input!=input.form.elements[i])
input.form.elements[i].checked = input.checked;
}
</script>
</head>

<body>
<form>
<input type="checkbox" name="Admin" value="Admin">User (Admin) Access<br>
<input type="checkbox" name="Articles" value="Articles">Articles Access<br>
<input type="checkbox" name="News" value="News">News Access<br>
<input type="checkbox" value="Select All" onchange="check(this)" />
</form>

glenngv
09-13-2004, 07:05 AM
That works fine but at the moment I need to name the tick boxes Differently so I can save the data induvidually.
(because at the moment echo $_POST['position']; is just showing one value and not all the vaules that were ticked).
But that would mean the tick all box wont work because they wont be named all the same

I don't know PHP but isn't it if you name form elements with [], PHP will automatically treat them as an array?

function check(objBtn){
var chks = objBtn.form.elements['position[]'];
var isChecked = (objBtn.value == 'Select All') ? true:false;
for (var i=0;i<chks.length;i++){
chks[i].checked = isChecked;
}
objBtn.value = (isChecked) ? "De-select All":"Select All";
}
...
<input type="checkbox" name="position[]" value="Admin" class="forminput" />User (Admin) Access<br />
<input type="checkbox" name="position[]" value="Articles" class="forminput" />Articles Access<br />
<input type="checkbox" name="position[]" value="News" class="forminput" />News Access<br />
<input type="button" value="Select All" class="forminput" onclick="check(this)" />