check checkbox submit

M

Mark

Hi,
I use javascript to force the user to
click checkbox when they click submit.
It works well when checkbox is more
than one. If only one checkbox, it doesn't
want me go through. I think my javascript
has some logic problem. but I don't know
how to modify it. Anyone can help me.
Thanks,



<html>
<head>
<title>Delete</title>
<SCRIPT LANGUAGE="JavaScript">
<!--
function DeleteInventory(){
for (var i=0; i<document.forms[0].batchdeletes.length; i++) {
if(document.forms[0].batchdeletes.checked){
if (confirm("This will delete Inventories you selected!")){
document.forms[0].action = "http://www.yahoo.com";
document.forms[0].submit();
return true;
}
else{
return false;
}
}
}
alert('You must check the checkbox');
return false;
}

// -->
</SCRIPT>
</head>
<body>
<form name="delete_inv" method="POST">
<p>
<input type="checkbox" name="batchdeletes" value="A123"><br>
<!--input type="checkbox" name="batchdeletes" value="A124"--><br>
<!--input type="checkbox" name="batchdeletes" value="A125"--><br>
<input type=button name=deletebutton value="Delete Selected Inventories"
onclick="return DeleteInventory();">
</p>
</form>
</body>
</html>
 
J

Jim Buyens

Howdy.

I think you'll need to count selected checkboxes, and then
test the count. For example:

cnt = 0;
for (i=0; i<document.forms[0].batchdeletes.length; i++) {
if(document.forms[0].batchdeletes.checked){
cnt = cnt + 1;
}
}
if (cnt > 0){
if (confirm(
"This will delete Inventories you selected!")){
document.forms[0].action = "http://www.yahoo.com";
document.forms[0].submit();
return true;
}else{
return false;
}
}else{
alert('You must select at least one checkbox');
return false;
}

Jim Buyens
Microsoft FrontPage MVP
http://www.interlacken.com
Author of:
*----------------------------------------------------
|\---------------------------------------------------
|| Microsoft Office FrontPage 2003 Inside Out
||---------------------------------------------------
|| Web Database Development Step by Step .NET Edition
|| Microsoft FrontPage Version 2002 Inside Out
|| Faster Smarter Beginning Programming
|| (All from Microsoft Press)
|/---------------------------------------------------
*----------------------------------------------------

-----Original Message-----
Hi,
I use javascript to force the user to
click checkbox when they click submit.
It works well when checkbox is more
than one. If only one checkbox, it doesn't
want me go through. I think my javascript
has some logic problem. but I don't know
how to modify it. Anyone can help me.
Thanks,



<html>
<head>
<title>Delete</title>
<SCRIPT LANGUAGE="JavaScript">
<!--
function DeleteInventory(){
if (confirm("This will delete Inventories you
selected!")){
document.forms[0].action = "http://www.yahoo.com";
document.forms[0].submit();
return true;
}
else{
return false;
}
}
}
alert('You must check the checkbox');
return false;
}

// -->
</SCRIPT>
</head>
<body>
<form name="delete_inv" method="POST">
<p>
<input type="checkbox" name="batchdeletes"
value="A123"> said:
<!--input type="checkbox" name="batchdeletes"
 
M

Mark

Jim,
The code didn't work.
Thanks,



<html>
<head>
<title>Delete</title>
<SCRIPT LANGUAGE="JavaScript">
<!--

function DeleteInventory(){
cnt = 0;
for (i=0; i<document.forms[0].batchdeletes.length; i++) {
if(document.forms[0].batchdeletes.checked){
cnt = cnt + 1;
}
}
if (cnt > 0){
if (confirm(
"This will delete Inventories you selected!")){
document.forms[0].action = "http://www.yahoo.com";
document.forms[0].submit();
return true;
}else{
return false;
}
}else{
alert('You must select at least one checkbox');
return false;
}
}
// -->
</SCRIPT>
</head>
<body>
<form name="delete_inv" method="POST">
<p>
<input type="checkbox" name="batchdeletes" value="A123"><br>
<!--input type="checkbox" name="batchdeletes" value="A124"--><br>
<input type=button name=deletebutton value="Delete Selected Inventories"
onclick="return DeleteInventory();">
</p>
</form>
</body>
</html>










Jim Buyens said:
Howdy.

I think you'll need to count selected checkboxes, and then
test the count. For example:

cnt = 0;
for (i=0; i<document.forms[0].batchdeletes.length; i++) {
if(document.forms[0].batchdeletes.checked){
cnt = cnt + 1;
}
}
if (cnt > 0){
if (confirm(
"This will delete Inventories you selected!")){
document.forms[0].action = "http://www.yahoo.com";
document.forms[0].submit();
return true;
}else{
return false;
}
}else{
alert('You must select at least one checkbox');
return false;
}

Jim Buyens
Microsoft FrontPage MVP
http://www.interlacken.com
Author of:
*----------------------------------------------------
|\---------------------------------------------------
|| Microsoft Office FrontPage 2003 Inside Out
||---------------------------------------------------
|| Web Database Development Step by Step .NET Edition
|| Microsoft FrontPage Version 2002 Inside Out
|| Faster Smarter Beginning Programming
|| (All from Microsoft Press)
|/---------------------------------------------------
*----------------------------------------------------

-----Original Message-----
Hi,
I use javascript to force the user to
click checkbox when they click submit.
It works well when checkbox is more
than one. If only one checkbox, it doesn't
want me go through. I think my javascript
has some logic problem. but I don't know
how to modify it. Anyone can help me.
Thanks,



<html>
<head>
<title>Delete</title>
<SCRIPT LANGUAGE="JavaScript">
<!--
function DeleteInventory(){
if (confirm("This will delete Inventories you
selected!")){
document.forms[0].action = "http://www.yahoo.com";
document.forms[0].submit();
return true;
}
else{
return false;
}
}
}
alert('You must check the checkbox');
return false;
}

// -->
</SCRIPT>
</head>
<body>
<form name="delete_inv" method="POST">
<p>
<input type="checkbox" name="batchdeletes"
value="A123"> said:
<!--input type="checkbox" name="batchdeletes"
value="A124"--> said:
<!--input type="checkbox" name="batchdeletes"
value="A125"--> said:
<input type=button name=deletebutton value="Delete Selected Inventories"
onclick="return DeleteInventory();">
</p>
</form>
</body>
</html>




.
 
J

Jim Buyens

Mark said:
Jim,
The code didn't work.
Thanks,



<html>
<head>
<title>Delete</title>
<SCRIPT LANGUAGE="JavaScript">
<!--

function DeleteInventory(){
cnt = 0;
for (i=0; i<document.forms[0].batchdeletes.length; i++) {
if(document.forms[0].batchdeletes.checked){
cnt = cnt + 1;
}
}
if (cnt > 0){
if (confirm(
"This will delete Inventories you selected!")){
document.forms[0].action = "http://www.yahoo.com";
document.forms[0].submit();
return true;
}else{
return false;
}
}else{
alert('You must select at least one checkbox');
return false;
}
}
// -->
</SCRIPT>
</head>
<body>
<form name="delete_inv" method="POST">
<p>
<input type="checkbox" name="batchdeletes" value="A123"><br>
<!--input type="checkbox" name="batchdeletes" value="A124"--><br>
<input type=button name=deletebutton value="Delete Selected Inventories"
onclick="return DeleteInventory();">
</p>
</form>
</body>
</html>



Whoops! I didn't notice that you had the "A124" checkbox commented
out. If you change it to

<input type="checkbox" name="batchdeletes" value="A124">

then batchdeletes will be an array and the code will work.

Jim Buyens
Microsoft FrontPage MVP
http://www.interlacken.com
Author of:
*----------------------------------------------------
|\---------------------------------------------------
|| Microsoft Office FrontPage 2003 Inside Out
||---------------------------------------------------
|| Web Database Development Step by Step .NET Edition
|| Microsoft FrontPage Version 2002 Inside Out
|| Faster Smarter Beginning Programming
|| (All from Microsoft Press)
|/---------------------------------------------------
*----------------------------------------------------
 

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

Top