?
Solved

How to retain checked checkboxes after submitting the form to same page in jsp ?

Posted on 2015-02-14
3
Medium Priority
?
2,503 Views
Last Modified: 2015-02-15
I have a form in JSP.
When I check any 3 checkboxes and submit the form I want the checked checkboxes to retain their state.
How to achieve this using javascript ?


<html>
<body>

<form action="">
<input type="checkbox" name="axes" value="Time">Time<br>
<input type="checkbox" name="axes" value="Product">Product<br> 
<input type="checkbox" name="axes" value="Location">Location<br> 
<input type="checkbox" name="axes" value="Sore">Store<br> 
<input type="submit" value="submit">
</form>

</body>
</html>

Open in new window

0
Comment
Question by:Pradip Shenolkar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 28

Accepted Solution

by:
rrz earned 2000 total points
ID: 40610982
Please try this code.
<%@ page import="java.util.*" %>
<html>
<body>
<% 
   String[] axes = request.getParameterValues("axes");
   if (axes != null) 
   {
   	  String check = "checked='checked'";
      List list = Arrays.asList(axes);
	  if(list.contains("Time"))pageContext.setAttribute("time", check);
	  if(list.contains("Product"))pageContext.setAttribute("product", check);	
	  if(list.contains("Location"))pageContext.setAttribute("location", check);
	  if(list.contains("Store"))pageContext.setAttribute("store", check);		 
   }
%>
<form>
<input type="checkbox" name="axes" value="Time" ${time}/>Time<br>
<input type="checkbox" name="axes" value="Product" ${product}/>Product<br> 
<input type="checkbox" name="axes" value="Location" ${location}/>Location<br> 
<input type="checkbox" name="axes" value="Store" ${store}/>Store<br> 
<input type="submit" value="submit">
</form>
</body>
</html>

Open in new window

0
 

Author Comment

by:Pradip Shenolkar
ID: 40611075
It solved the problem but it would be more helpful to me from my project perspective if the solution is achieved using javascript.
0
 
LVL 28

Expert Comment

by:rrz
ID: 40611090
When the client submits the form, he creates a new request. The Javascript values from the previous page are lost. To use Javascript, you would have to store the data somewhere. You could use cookies or use localStorage (HTML5). Look at
http://www.tutorialspoint.com/javascript/javascript_cookies.htm
http://www.thesitewizard.com/javascripts/cookies.shtml
http://www.smashingmagazine.com/2010/10/11/local-storage-and-how-to-use-it/     
https://robert.accettura.com/blog/2012/01/17/localstorage-with-cookie-fallback/
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
Suggested Courses

770 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