multiple checkboxes help needed

R

Ralph Freshour

I have multiple checkbox's created with an array name because I have
many on the same web page - their names are like:

frm_chk_delete[0]
frm_chk_delete[1]
frm_chk_delete[2]
frm_chk_delete[3]
etc.

Here is my code line that creates each checkbox (the php variable get
incremented of course):

<INPUT TYPE="checkbox" NAME="frm_chk_delete[<?php echo
$php_delete_counter;?>]">Delete

When I click on the Delete button I want to call a JS routine to check
that at least one checknbox has been checked and marked for deletion
when the form submit is called my php file will detect and delete the
checkboxes accordingly.

I'd like to use JS before the submit and make sure at least one of the
checkboxes is checked - how do I loop through these array name
checkboxes in javascript to check their states before submitting?

Thanks...
 
L

Lasse Reichstein Nielsen

Ralph Freshour said:
frm_chk_delete[0]
frm_chk_delete[1]
frm_chk_delete[2]
frm_chk_delete[3] ....
<INPUT TYPE="checkbox" NAME="frm_chk_delete[<?php echo
$php_delete_counter;?>]">Delete ....
I'd like to use JS before the submit and make sure at least one of the
checkboxes is checked - how do I loop through these array name
checkboxes in javascript to check their states before submitting?

---
<script type="text/javascript">
function isOneSet(form) {
for (var i=0;i<numberOfCheckboxes;i++) {
if (form.elements["frm_chk_delete["+i+"]"].checked) {
return true;
}
}
return false;
}
</script>
 
R

Ralph Freshour

How do I set the "numberOfCheckboxes"?

To test the code I replaced "numberOfCheckboxes" with 2 and that
worked but then it choked on the if statement.

I'm using win98 and IE 6.0

Thanks...

Ralph Freshour said:
frm_chk_delete[0]
frm_chk_delete[1]
frm_chk_delete[2]
frm_chk_delete[3] ...
<INPUT TYPE="checkbox" NAME="frm_chk_delete[<?php echo
$php_delete_counter;?>]">Delete ...
I'd like to use JS before the submit and make sure at least one of the
checkboxes is checked - how do I loop through these array name
checkboxes in javascript to check their states before submitting?

---
<script type="text/javascript">
function isOneSet(form) {
for (var i=0;i<numberOfCheckboxes;i++) {
if (form.elements["frm_chk_delete["+i+"]"].checked) {
return true;
}
}
return false;
}
</script>
 
M

Michael Winter

Ralph Freshour wrote on 14 Dec 2003 at Sun, 14 Dec 2003 16:00:11
GMT:
I have multiple checkbox's created with an array name because I
have many on the same web page - their names are like:

frm_chk_delete[0]
frm_chk_delete[1]
frm_chk_delete[2]
frm_chk_delete[3]
etc.

Here is my code line that creates each checkbox (the php
variable get incremented of course):

<INPUT TYPE="checkbox" NAME="frm_chk_delete[<?php echo
$php_delete_counter;?>]">Delete

When I click on the Delete button I want to call a JS routine to
check that at least one checknbox has been checked and marked
for deletion when the form submit is called my php file will
detect and delete the checkboxes accordingly.

I'd like to use JS before the submit and make sure at least one
of the checkboxes is checked - how do I loop through these array
name checkboxes in javascript to check their states before
submitting?

It would be easier if they all had the same name, but it can be done
as it is.

<FORM ... onsubmit="return checkBoxes(this)">

function checkBoxes(form) {
var numControls = form.elements.length;
var checks = 0;

// Check every control in the form
for (var i = 0; i < numControls; ++i) {
var element = form.elements;

// If the name begins, frm_chk_delete[ ...
if ('frm_chk_delete[' == element.name.substr(0, 15)) {
// ...see if it's checked, and increment counter if it is
if (element.checked) checks++;
}
}
// If none of boxes have been checked, alert the user and cancel
// the submission
if (!checks) {
window.alert('Please check at least one box before submitting');
return false;
}

// If code reaches here, at least one box was checked, so allow
// the form to submit
return true;
}

If it's not guaranteed that only checkboxes will have names that
start with 'frm_chk_delete[', you should also check to make sure that
the element being inspected is a checkbox (use element.type ==
'checkbox').

Hope that helps,

Mike
 
L

Lasse Reichstein Nielsen

Ralph Freshour said:
How do I set the "numberOfCheckboxes"?

I don't know. It is the number of checkboxes you have created, so I'll
assume you insert it with PHP somehow.
To test the code I replaced "numberOfCheckboxes" with 2 and that
worked but then it choked on the if statement.

"choked"? Not a useful error report :)
You'll have to be a little more informative, or we won't be able
to help. What is the *exact* error message? Can we see the page?

/L
 
R

Ralph Freshour

Sorry - the error is in the lower left corner of my browser: "Error on
page"

That's all it says - the code below displays 1 and 2 and give the
error msg - so it must be in the if statement:

function atLeastOneChecked(form){
alert("1");
for (var i=0;i<2;i++) {
alert("2");
if (form.elements["frm_chk_delete["+i+"]"].checked) {
alert("3");
return true;
}
}
alert("Please check at least one box!");
return false;
}
 
R

Ralph Freshour

this line is giving me "Error on page" msg in lower left corner of my
browser:

var numControls = form.elements.length;

???



Ralph Freshour wrote on 14 Dec 2003 at Sun, 14 Dec 2003 16:00:11
GMT:
I have multiple checkbox's created with an array name because I
have many on the same web page - their names are like:

frm_chk_delete[0]
frm_chk_delete[1]
frm_chk_delete[2]
frm_chk_delete[3]
etc.

Here is my code line that creates each checkbox (the php
variable get incremented of course):

<INPUT TYPE="checkbox" NAME="frm_chk_delete[<?php echo
$php_delete_counter;?>]">Delete

When I click on the Delete button I want to call a JS routine to
check that at least one checknbox has been checked and marked
for deletion when the form submit is called my php file will
detect and delete the checkboxes accordingly.

I'd like to use JS before the submit and make sure at least one
of the checkboxes is checked - how do I loop through these array
name checkboxes in javascript to check their states before
submitting?

It would be easier if they all had the same name, but it can be done
as it is.

<FORM ... onsubmit="return checkBoxes(this)">

function checkBoxes(form) {
var numControls = form.elements.length;
var checks = 0;

// Check every control in the form
for (var i = 0; i < numControls; ++i) {
var element = form.elements;

// If the name begins, frm_chk_delete[ ...
if ('frm_chk_delete[' == element.name.substr(0, 15)) {
// ...see if it's checked, and increment counter if it is
if (element.checked) checks++;
}
}
// If none of boxes have been checked, alert the user and cancel
// the submission
if (!checks) {
window.alert('Please check at least one box before submitting');
return false;
}

// If code reaches here, at least one box was checked, so allow
// the form to submit
return true;
}

If it's not guaranteed that only checkboxes will have names that
start with 'frm_chk_delete[', you should also check to make sure that
the element being inspected is a checkbox (use element.type ==
'checkbox').

Hope that helps,

Mike
 
L

Lasse Reichstein Nielsen

Ralph Freshour said:
Sorry - the error is in the lower left corner of my browser: "Error on
page"

Your browser is some version of Internet Explorer, correct?
You can click on the error icon and get a better explanation (not good,
just better).
You can turn error messages on in:
Tools > Internet Options > Advanced > Browsing :
Display a notification about every scrip error.
That's all it says - the code below displays 1 and 2 and give the
error msg - so it must be in the if statement:
if (form.elements["frm_chk_delete["+i+"]"].checked) {

If this fails, it is because something doesn't have the correct value.

Either "form" is not pointing to the correct form, or it has no
element called "frm_chk_delete[0]". Try adding alerts before the if:
alert(form);
alert(form.elements["frm_chk_delete[0]"]);

Check that the form is written correctly.

And please don't top post! At least trim your quotes.

/L
 
R

Ralph Freshour

Ralph Freshour said:
Sorry - the error is in the lower left corner of my browser: "Error on
page"

Your browser is some version of Internet Explorer, correct?
You can click on the error icon and get a better explanation (not good,
just better).
You can turn error messages on in:
Tools > Internet Options > Advanced > Browsing :
Display a notification about every scrip error.
That's all it says - the code below displays 1 and 2 and give the
error msg - so it must be in the if statement:
if (form.elements["frm_chk_delete["+i+"]"].checked) {

If this fails, it is because something doesn't have the correct value.

Either "form" is not pointing to the correct form, or it has no
element called "frm_chk_delete[0]". Try adding alerts before the if:
alert(form);
alert(form.elements["frm_chk_delete[0]"]);

Check that the form is written correctly.

And please don't top post! At least trim your quotes.

/L


Thanks for the info about turning on the details of the err msg in IE
- I saw the problem was in

var numControls = form.elements.length;

so in my calling statement I changed it from (this) to (this.form) and
then it worked ok.

Thanks for help everyone...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top