View Full Version : help in Javascript for checkedbox values
Thimo
10-24-2002, 07:11 AM
<script language="JavaScript">
function Boxcheck()
{
if(item_form.add.checked)
return true;
else
alert("You have not checked any items.")
return false;
}
</script>
</head>
<title>Item List for bookstore view</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
<body bgcolor="#FFCCFF" background="../images/lblue132.gif">
<center>
<h1><i><span style="letter-spacing: 1pt"><b><font face="Impact" size="5"><sup><font size="+7">the
</font></sup></font><font face="Impact" size="+7">E-BookStore</font></b></span></i><br>
Stationery Item Menu</h1>
<h3>Welcome <%= Session("PersonName")%></h3>
<p><font size="4" face="Impact">Click Item Code to Add Items</font></p>
<p align="left">* Please <a href="ViewItems.asp" target=_parent class="tlink">[
refresh ]</a> this page for up-to-date information.<br>
<%If Not rsItems.EOF Then %>
</p>
<form name="item_form" method="post" action="AddShoppingcart.asp" onSubmit="return Boxcheck()">
<br>
<table border="1" align="center">
<tr>
<th width="68">Item Code</th>
<th width="73">ItemName</th>
<th width="58">Quantity</th>
<th width="50">Price</th>
<th width="67">Click to Add</th>
</tr>
<%Do While Not rsItems.EOF %>
<tr align=center>
<td><%=rsItems("ItemCode")%></td>
<td align=left><%=rsItems("ItemName")%></td>
<td><%=rsItems("Quantity")%> </td>
<td><%=FormatCurrency(rsItems("Price"))%> </td>
<td><input type="checkbox" name="add" value=<%=rsItems("ItemID")%>></td>
<tr>
<%rsItems.MoveNext%>
<%loop%>
<%End If%>
</table>
<p>
<input type="submit" name="Submit" value="Add Items to Cart">
</p>
</form>
<p align="left"> </p>
</center>
<div align="left">
<%rsItems.close%>
</div>
</body>
</html>
i wish to validate the form, promopt the user an alert msg if he didn;t checked any of the boxes.....please advice...
my form name is "item_form"
checkbox = "add" value = <%rs("ItemID")%>
is the value overwritten by the ASP Code that it cannot be checked wther the user got tick the boxes?
Thimo
10-24-2002, 08:11 AM
any1 please help?
jolietjake
10-24-2002, 10:08 AM
Well, My first suggest is to change the submit button. Put a input type="button" and a onclick="validateForm(this.form)"
then, put this function in your code:
function validateForm(f)
{
var flag=0;
for (var i=0; i<f.elements.length; i++)
if (f.elements[i].value !="")
flag==1;
if (flag==0)
alert (Hey! check some checkboxes!);
else
f.submit();
}
I think it is what you need, isn't it?
Jolietjake, Master of Blues & Rythm:cool:
Thimo
10-24-2002, 10:46 AM
if i change to a button?...
wll the button still actions to the next page i wanted?
izzit the f.submit?
thanks alot man....
waj_muller
10-24-2002, 11:17 AM
jolietjake has the right idea.To get your page flipping to the next without losing any info.....Its a tricky bit.
In your form tag call the check function and have it return a true or flase like so..
<form onsubmit="return function()">
but keep the submit button you will want to keep the form object info.
The idea with jolietjake's function is to count all the checkboxes and resolve there status.
If you have more than a couple of diffrent checkbox functions on a page give them id's each object range with an id,so in your case all you objects will have individual names but one id.This way you only look at one id range at one time.
this method allows you to valadate very complex forms without any problem.
waj_muller
10-24-2002, 01:12 PM
Try this this makes a good solution.........
<HTML>
<HEAD>
<SCRIPT LANGUAGE=javascript>
<!--
//This will allow you to pass all the values to your next page.
function selectall_onclick() {
var ss=document.whatever.ch.length;
alert(ss);
for (d=0;d<ss;d++)
{
if(!(document.whatever.ch[d].status))
{
return(false);
}
}
return(true);
}
//-->
</SCRIPT>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<form name="whatever" method="post" onsubmit="return selectall_onclick()" action="test.asp">
<input type="checkbox" name="adds<%'=rsitem("id")%>" id="ch" value="<%'=rsitem("id")%>">
<input type="checkbox" name="adds<%'=rsitem("id")%>" id="ch" value="<%'=rsitem("id")%>">
<P> </P>
<input type="submit" Value="submit">
</form>
</BODY>
</HTML>
Thimo
10-25-2002, 09:04 AM
i have tried jolietjake's method
but the thing is that my checkboxes all have values of numbers on it, taken from my database value = <%=rs(ItemID)%>
is there any way to compare without using the values of the checkboxes to check wether the box is checked anot ?
if (f.elements[i].value !="")
flag==1;
if (flag==0)
alert ("check some checkboxes!");
else
f.submit();
joilet, you are trying to check wether the checkbox values is null anot right?...if the value is null it will alert..
but strange ...all checkboxes have values ....it should submit.... the result is even if dun check or checked the boxes ,....it will also prompt the alert msgs...
please help..
thanks
jolietjake
10-25-2002, 09:22 AM
try
...
if (f.elements[i].checked!=false)
...
I tried it and it works better
Jolietjake. Master of Blues & Rythm:cool:
waj_muller
10-25-2002, 09:24 AM
Yes you cannot check in your case for values.
To check them use
document.formname.checkboxname.status;
this will return a true if checked and a false if not checked.
Sorry the code i gave you is not complete,my boss was breathing over my neck to finish some damm fancy database function,yahh like i wanted to...
Hope it help you i will post the correct code soon,just need my boss to back off..
Thimo
10-25-2002, 09:29 AM
its doesn't seems to work.......hmm....... but anyway thanks....
<script language="JavaScript">
function validateForm(f)
{
var flag=0;
for (var i=0; i<f.elements.length; i++)
if (f.elements[i].checked !=false)
flag==1;
if (flag==0)
alert ("check some checkboxes!");
else
f.submit();
<%If Not rsItems.EOF Then %>
</p>
<form name="item_form" method="post" action="AddShoppingcart.asp">
<br>
<table border="1" align="center">
<tr>
<th width="68">Item Code</th>
<th width="73">ItemName</th>
<th width="58">Quantity</th>
<th width="50">Price</th>
<th width="67">Click to Add</th>
</tr>
<%Do While Not rsItems.EOF %>
<tr align=center>
<td><%=rsItems("ItemCode")%></td>
<td align=left><%=rsItems("ItemName")%></td>
<td><%=rsItems("Quantity")%> </td>
<td><%=FormatCurrency(rsItems("Price"))%> </td>
<td><input name="add" type="checkbox" value=<%=rsItems("ItemID")%> ></td>
<tr>
<%rsItems.MoveNext%>
<%loop%>
<%End If%>
</table>
<p>
<input name="button" type="button" onClick="validateForm(this.form)" value="Add Items to Cart">
</p>
</form>
actually i juz want to come out with an alert box if the user nver check any boxes...... nver thought it will be so difficult...haiz.....
jolietjake
10-25-2002, 09:35 AM
Here's the page I use to try the script, and it works!! I don't know wheres the problem!? Perhaps you have syntax errors?
<html>
<head>
<script language="javascript">
<!--
function validar(f)
{
var flag=0;
for (var i=0;i<f.elements.length;i++)
{
if (f.elements[i].checked !=false)
{
flag=1;
alert(f.elements[i].value);
}
}
if (flag==0)
alert ("check some checkboxes!");
else
alert("send");
}
//-->
</script>
</head>
<body>
<form>
<input type="checkbox" name="checkbox1" value="1">
<input type="checkbox" name="checkbox2" value="2">
<input type="checkbox" name="checkbox3" value="3">
<input type="checkbox" name="checkbox4" value="4">
<input type="button" name="boton" value="validate" onclick="validar(this.form)">
</form>
</body>
</html>
Thimo
10-25-2002, 09:40 AM
thanks man joilet.....
i managed to get the thing right....lol
my deepest gratitude.....
if i have some more problems, can i ask you?
regards
jolietjake
10-25-2002, 09:44 AM
You can ask me when you want:thumbsup: I gladly answer your questions, but remember, I'm a Junior Member :D
JolietJake. Master of Blues & Rythm:cool:
glenngv
10-25-2002, 09:47 AM
the number of checkbox could be zero or many, depending on the result of records retrieved from the database.
so the submit button must be inside the "If Not rsItems.EOF" condition
<html>
<head>
<title>Item List for bookstore view</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
function Boxcheck()
{
f = document.item_form;
for (var i=0; i<f.elements.length; i++) {
if (f.elements[i].type=="checkbox" && f.elements[i].checked) return true;
}
alert("You have not checked any items.");
return false;
}
</script>
</head>
<body bgcolor="#FFCCFF" background="../images/lblue132.gif">
<center>
<h1><i><span style="letter-spacing: 1pt"><b><font face="Impact" size="5"><sup><font size="+7">the
</font></sup></font><font face="Impact" size="+7">E-BookStore</font></b></span></i><br>
Stationery Item Menu</h1>
<h3>Welcome <%= Session("PersonName")%></h3>
<p><font size="4" face="Impact">Click Item Code to Add Items</font></p>
<p align="left">* Please <a href="ViewItems.asp" target=_parent class="tlink">[
refresh ]</a> this page for up-to-date information.<br>
<%If Not rsItems.EOF Then %>
</p>
<form name="item_form" method="post" action="AddShoppingcart.asp" onSubmit="return Boxcheck()">
<br>
<table border="1" align="center">
<tr>
<th width="68">Item Code</th>
<th width="73">ItemName</th>
<th width="58">Quantity</th>
<th width="50">Price</th>
<th width="67">Click to Add</th>
</tr>
<%Do While Not rsItems.EOF %>
<tr align=center>
<td><%=rsItems("ItemCode")%></td>
<td align=left><%=rsItems("ItemName")%></td>
<td><%=rsItems("Quantity")%> </td>
<td><%=FormatCurrency(rsItems("Price"))%> </td>
<td><input type="checkbox" name="add" value=<%=rsItems("ItemID")%>></td>
</tr>
<%
rsItems.MoveNext
loop
%>
</table>
<p>
<input type="submit" name="Submit" value="Add Items to Cart">
</p>
<%
else
Response.write("No items to display.")
End If%>
</form>
<p align="left"> </p>
</center>
<div align="left">
<%rsItems.close%>
</div>
</body>
</html>
Thimo
10-25-2002, 09:54 AM
thanks to glenn for all the effort.....and the help you gave me on others threeads i made....
thanks alot..... if i have any other problems...i will ask you again...
my project is about to finish......
glenn? about my other thread regarding the serverside scripting and clientside scripting.... how am i going to implement it?.... to prompt the user that he had entered a quantity that is higher then the quantity available...
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.