We help IT Professionals succeed at work.

Very basic HTML checkbox problem

pcalabria
pcalabria asked
on
214 Views
Last Modified: 2012-02-15
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>
Comment
Watch Question

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

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.