Solved

Why the checkbox state is not retained in following code ?

Posted on 2015-02-15
2
116 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adding Row and Cell on a table 14 25
How can I make a javascript to show drop down 4 22
Dynamic Dropdowns 15 32
Character counter breaks after adding EmojiOne Area 4 19
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

839 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