admin管理员组文章数量:1402920
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" ".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)">
<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.
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/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)">
<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.
Share Improve this question edited Dec 17, 2010 at 11:49 BalusC 1.1m376 gold badges3.7k silver badges3.6k bronze badges asked Dec 17, 2010 at 5:06 SumithraSumithra 6,71719 gold badges54 silver badges50 bronze badges 1- Are you getting errors, no change, wrong changes? Please tell us what is happening, it will help us determine what the problem is. – Surreal Dreams Commented Dec 17, 2010 at 5:12
2 Answers
Reset to default 4You'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.
本文标签: javascriptgetting checked values of a check box and send it to a servletStack Overflow
版权声明:本文标题:javascript - getting checked values of a check box and send it to a servlet - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744345256a2601719.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论