Link to home
Start Free TrialLog in
Avatar of pcalabria
pcalabriaFlag for United States of America

asked on

Very basic HTML checkbox problem

Hello Experts, thanks in advance.
I've new to Javascript and just can't figure this out!

I have an Order Form that consists of a variable number of rows.

The customer simply checks a checkbox to select which items he wants.

The problem I am having is that if a checkbox is not checked, then no value is returned.

Here's some sample code.  While the code below only includes two line items, the actual number may vary from 1 to any number of rows.

mypage.asp must have code to extract the part number and qty for each item selected.

If there's a way for a checkbox to return not checked this job would be easy.

My code on the mypage.asp loops through the querystring collection, but returns an error when a value of checkbox that is not checked is encountered.

''''Code Below"""
<form action="mypage.asp" method="get">

Qty<input name="Qty" type="text" />
Part Number<input name="PartNumber" type="text" />
<input type="checkbox" name="buyme" value="yes" /><br /><br />

Qty<input name="Qty" type="text" />
Part Number<input name="PartNumber" type="text" />
<input type="checkbox" name="buyme" value="yes" /><br /><br />

<input name="Update" type="submit" />
</form>
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <script>
function ex(obj)
{
if(obj.checked)
alert('checked..'+obj.value)
else
alert('not checked..'+obj.value)

}
  </script>
 </HEAD>

 <BODY>
  <form action="mypage.asp" method="get">

Qty<input name="Qty" type="text" />
Part Number<input name="PartNumber" type="text" />
<input type="checkbox" name="buyme" value="yes"  onclick='ex(this)'/><br /><br />

Qty<input name="Qty" type="text" />
Part Number<input name="PartNumber" type="text" />
<input type="checkbox" name="buyme" value="yes" onclick='ex(this)'/><br /><br />

<input name="Update" type="submit" />
</form>


 </BODY>
</HTML>

Open in new window

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <script>
function ex(obj)
{
if(obj.checked)
alert('checked..'+obj.value)
else
alert('not checked..'+obj.value)

}
  </script>
 </HEAD>

 <BODY>
  <form action="mypage.asp" method="get">

Qty<input name="Qty" type="text" />
Part Number<input name="PartNumber" type="text" />
<input type="checkbox" name="buyme" value="yes"  onclick='ex(this)'/><br /><br />

Qty<input name="Qty" type="text" />
Part Number<input name="PartNumber" type="text" />
<input type="checkbox" name="buyme" value="yes" onclick='ex(this)'/><br /><br />

<input name="Update" type="button" />
</form>


 </BODY>
</HTML>

Open in new window

this script wil give you the checkboxes that are not checked

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <script>
function ex(obj)
{

for(var i=0;i<document.getElementsByName("buyme").length;i++)
{
if(!document.getElementsByName("buyme")[i].checked)
alert('not checked..'+document.getElementsByName("buyme")[i].value)

}

}
  </script>
 </HEAD>

 <BODY>
  <form action="mypage.asp" method="get">

Qty<input name="Qty" type="text" />
Part Number<input name="PartNumber" type="text" />
<input type="checkbox" name="buyme" value="1" /><br /><br />

Qty<input name="Qty" type="text" />
Part Number<input name="PartNumber" type="text" />
<input type="checkbox" name="buyme" value="2"/><br /><br />

<input name="Update" type="button" value='button' onclick='ex()'/>
</form>


 </BODY>
</HTML>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of pcalabria
pcalabria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pcalabria

ASKER

I solved the problem by naming the checkbox variable using the row sequence number as its name, and then creating a recordset of just the checkbox items on the page that recieved the get.

So lets say there were 10 rows and two were selected, the recordset of checkbox items would include just two items, and the value of these two items provide the index numbers of the selected rows.