By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

multiple checkboxes help needed

P: n/a
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...

Jul 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Ralph Freshour <ra***@primemail.com> writes:
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>
---
and
---
<form ... onsubmit="if (!isOneSet(this)) {alert('SET ONE!');return false;}">
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
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...

On Sun, 14 Dec 2003 17:27:26 +0100, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
Ralph Freshour <ra***@primemail.com> writes:
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>
---
and
---
<form ... onsubmit="if (!isOneSet(this)) {alert('SET ONE!');return false;}">
---

/L


Jul 20 '05 #3

P: n/a
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[i];

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

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk")
Jul 20 '05 #4

P: n/a
Ralph Freshour <ra***@primemail.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

P: n/a
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;
}


On Sun, 14 Dec 2003 19:00:45 +0100, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
Ralph Freshour <ra***@primemail.com> writes:
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


Jul 20 '05 #6

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

var numControls = form.elements.length;

???

On Sun, 14 Dec 2003 17:55:13 GMT, Michael Winter
<M.******@blueyonder.co.invalid> wrote:
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[i];

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


Jul 20 '05 #7

P: n/a
Ralph Freshour <ra***@primemail.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #8

P: n/a
On Sun, 14 Dec 2003 21:46:52 +0100, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
Ralph Freshour <ra***@primemail.com> writes:
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...
Jul 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.