getting checked values of a check box and send it to a servlet
I have to display a list of users with checkbox in its front. I now want to select some of the names and delete it in database. I have written code to display users. I don't know how to get the selected values and delete it. Please help me here is what I have tried
<%@ 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">
<html>
<head>
<%String user=request.getParameter("Users"); %>
<script type="text/javascript">
var chk="dummy";
function doSubmit(theClick){
window.open("AddUser.jsp");
return false;
}
function setValue(chVal)
{
chk=chk+","+chVal;
alert(chk);
}
function doDelete(theClick){
document.buttons.action = "AddUserServlet?type=delete&name="+chk;
alert(chk);
document.buttons.submit();
}
</script>
</head>
<body>
<center>
<!-- action="AddUserServlet"-->
<%
try{
String User[]=user.trim().split(" ");
for(int i=0;i<User.length;i++){
out.println("<html>");
out.println("<body>");
out.println("<form>");
out.println("<input type='checkbox' value='User[i]' onClick='setValue(this.value)' name='user'>"+User[i]+"</input>");
//out.println("<input type='checkbox' value='User'+i+ name='user1'>"+User[i]+"</br>"+"</form>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
//out.println("<input type='checkbox' value='User'+i+ name='user1'>"+User[i]+"</br>"+"</form>");
}
}catch(Exception e){
e.printStackTrace();
}
%>
<!--
<form method="Post" name="buttons">
<input type='checkbox' value='User' onClick="setValue(this.value)" name='user1'></input>
<input type='checkbox' value='User1' onClick="setValue(this.value)" name='user1'></input>
<input type='checkbox' value='User2' onClick="setValue(this.value)" name='user1'></input>
<div id="add"></div>
</form>-->
<form method="Post" name="add">
<input type="submit" value="ADD" onclick="return doSubmit(this)">
&开发者_如何学运维lt;input type="submit" value="MODIFY" onClick="return doSubmit(this)">
<input type="submit" value="DELETE" onClick="return doDelete(this)">
</form>
</center>
</body>
</html>
The code above displays list of names along with checkboxes. But returns value "user" and not the name in database. Also if I uncheck a name it again returns user.
[*]abc-----> returns dummy,user
if i uncheck this []user returns dummy,user0,user1
I need get the username list.
You're printing a whole new <html>
and <form>
around every single checkbox. Your HTML ends up in browser like as:
<html>
<head></head>
<body>
<html><body><form><input type="checkbox"></form></body></html>
<html><body><form><input type="checkbox"></form></body></html>
<html><body><form><input type="checkbox"></form></body></html>
<form><input type="submit"></form>
</body>
</html>
This is syntactically invalid HTML. You need to rewrite your code so that all checkboxes and the submit button ends up in the same form:
<html>
<head></head>
<body>
<form>
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="submit">
</form>
</body>
</html>
Then you also don't need those ugly JavaScript workarounds. You just give the checkboxes the same name, but a different value. This way you can just grab the checked values by HttpServletRequest#getParameterValues()
.
String[] users = request.getParameterValues("user");
You'll need to assign different names to each checkbox per user. This way, the servlet that gets the POSTed form can check each checkbox name (e.g. user1
...userX
) and if its value is set then you can delete the corresponding user. Note that if the checkbox is not set then there will be no form encoded parameter sent for that name, so you could really just check to see which names were sent and delete those.
精彩评论