Solved

Why the checkbox state is not retained in following code ?

Posted on 2015-02-15
2
111 Views
Last Modified: 2015-02-18
When I check few checkboxes and submit the form, all checkboxes are set to unchecked state.
What am I doing wrong ?

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String fruits[]= request.getParameterValues("fruit");
pageContext.setAttribute("fruits", fruits);
if(fruits != null)
{
%>
<h4>I likes fruit/s mostly</h4>
<ul>
<%
for(int i=0; i<fruits.length; i++)
{
%>
<li><%=fruits[i]%></li>
<%
}
%>
</ul>
<%
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
var flag=0;
function checkBoxValidation()
{
for(var i=0; i < document.form1.fruit.length; i++)
{
if(document.form1.fruit[i].checked)
{
	document.form1.fruit[i].checked=true;
flag++;
}
}

if(flag==0)
	{
	alert("select smthing");
	}
	
}
</script>
<title>JSP Multiple Checkbox</title>
</head>
<body>
<form name="form1" onsubmit="checkBoxValidation()">
<h3>Select your favorite Fruits</h3>
<p><input type="checkbox" name="fruit" value="Mango"/>Mango</p>
<p><input type="checkbox" name="fruit" value="Apple"/>Apple</p>
<p><input type="checkbox" name="fruit" value="Grapes"/>Grapes</p>
<p><input type="checkbox" name="fruit" value="Papaya"/>Papaya</p>
<p><input type="checkbox" name="fruit" value="Lychee"/>Lychee</p>
<p><input type="checkbox" name="fruit" value="Pineapple"/>Pineapple</p>
<p><input type="submit" value="submit"/>
</form>

</body>
</html>

Open in new window

0
Comment
Question by:Pradip Shenolkar
2 Comments
 
LVL 10

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 250 total points
ID: 40611674
You are harvesting the values, but when writing out your checkboxes you don't check if that value was submitted and setting the checkbox attribute of checked.  You should have a loop similar to the top checking if the array of fruits contains the checkbox you are displaying and add checked attribute if it does.
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 250 total points
ID: 40612108
Common checkbox names need to be submitted as an array.

<p><input type="checkbox" name="fruit[]" value="Mango"/>Mango</p>
<p><input type="checkbox" name="fruit[]" value="Apple"/>Apple</p>
...

Otherwise only the last one checked will come through on submit.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now