473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

multiple checkboxes help needed

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
8 2798
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ralph Freshour | last post by:
I'm using MySQL to display data on a web page - fitting about 10 records of data per screen - I'm including a checkbox with each record so that I can check it and when the submit button is clicked...
3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
1
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware ...
2
by: Ralph Freshour | last post by:
I have a function that I call to check or uncheck all checkboxes on a form - I use a 'master' checkbox to do this much like hotmail has to check all mail messages - the code works fine if I name my...
6
by: jeffsnox | last post by:
Hi, I have multiple checkboxes on the same form as follows: <input type='checkbox' name='cbtype' value='1'> <input type='checkbox' name='cbtype' value='2'> <input type='checkbox'...
1
by: projectjakecs | last post by:
Hi, I am working on a ms access database and I am having trouble using a form to create new records in an associative table. Here is the breakdown of my database: Main Table - Computer...
4
by: ramapv | last post by:
can i highlight a checkbox from a group of checkbox with particular name which is given as a search key. I am having a list of checkboxes and i have to select some of them and form a group.but i'm...
3
by: swb76 | last post by:
Hi, I have 6 queries in Access that run great. They need to be run in sequence with the first 5 queries writing to tables and the sixth one pops up the final results in datasheet view. Now, how...
5
by: TechnoAtif | last post by:
Hi All..'mAtif..i've got stuck within checkboxes these days..i've got many input items like checkboxes,textarea and along with them there are many checkboxes...all the data except the checkbox's is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.