jsp获取复选框的值并保存数据库

从JSP页面获取复选框的values值,通过定义数组,将其值存入数组之中,并显示出来

最简单的就是

 代码如下 复制代码

String strLove = "";
String[] strLoves = (String[])request.getParameterValues("love");

//通过循环读取每个选中项
for (String love : strLoves)
{
    strLove = strLove + love + ",";
}

strLove = strLove.substring(0,strLove.length()-1);
 

html页面

 代码如下 复制代码

<form action="02.jsp" method="post">
        姓名:<input type="text" name="uname" />
        <p>
        擅长技术:
        <input type="checkbox" name="tech" value="J2EE" />J2EE
        <input type="checkbox" name="tech" value=".NET" />.NET
        <input type="checkbox" name="tech" value="ASP" />ASP
        <input type="checkbox" name="tech" value="PHP" />PHP
        <p>
        <input type="submit" value="提交" />
</form>

jsp处理页面

 代码如下 复制代码

<%@page contentType="text/html;charset=GB2312" %>
<html>
  <head>
    <title>02.jsp</title>
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
 
  <body>
      <%
          request.setCharacterEncoding("GB2312");
          String Name=request.getParameter("uname");
          //获得参数数组
          String Tech[]=request.getParameterValues("tech");
      %>
      <h1>姓名:<%=Name %></h1>
      <h1>擅长技术:
      <%
          //输出数组
          int i;
          for(i=0;i<Tech.length;i++)
          {
      %>
              <%=Tech[i] %>
      <%
          }
      %>
      </h1>
  </body>
</html>

上面的代码不分中英文的,如果选项的值中含有中文,在JSP中的处理代码如下:

 代码如下 复制代码

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>

<%!
public String chinese(String sText)
{
    try
    {
        return new String(sText.getBytes("iso-8859-1"),"gbk");
    }
    catch(Exception e)
    {
        return sText;
    }
}
%>

<%
String strLove = "";
String[] strLoves = (String[])request.getParameterValues("love");

//通过循环读取每个选中项
for (String love : strLoves)
{
    strLove = strLove + chinese(love) + ",";
}

strLove = strLove.substring(0,strLove.length()-1);
%>

jsp 获得checkbox值并写入数据库,可通过以下方法:

 代码如下 复制代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
        <title>test</title>
    </head>

    <body>
<form name="myform" id="myform" method="post" action="CorAdd.jsp">  
        <label>
            <input type="checkbox" name="CorpMode" value="1" />物品
        </label>
        <label>
            <input type="checkbox" name="CorpMode2" value="2" />人头
        </label>
        <label>
            <input type="checkbox" name="CorpMode2" value="3" />其他
        </label>      
        <input name="" type="button"onclick="document.myform.action='Submit.jsp?menu=add'" />
</form>
    </body>
</html>

保存页面jsp程序

 代码如下 复制代码

<%@ page contentType="text/html; charset=utf-8" language="java" import = "java.util.*, java.text.*,java.sql.*"%>

<%
    if(menu.equals("add"))
{

    String tmp="";
    String SCorpMode="";
    String[] CorpMode = request.getParameterValues("CorpMode");
    if(CorpMode.length>0)
        {
            for(int i=0;i<CorpMode.length;i++)
            {  
                SCorpMode=SCorpMode+CorpMode+",";  
            }
            SCorpMode=SCorpMode.substring(0,SCorpMode.length()-1);//去掉SID中的最后一个逗号  
        }

tmp ="insert into Corporation (CorpMode) values('+SCorpMode+');

    if(dbc.executeUpdate(tmp)>=0)
       out.println("<script>alert('信息添加成功!');location.href='CorpAdd.jsp'</script>");
    else
        
        //out.println("<script>alert('添加失败!');location.href='CorpAdd.jsp'</script>");
        out.print(tmp);
}
}
%>

时间: 2024-08-29 04:48:12

jsp获取复选框的值并保存数据库的相关文章

JS获取复选框的值,并传递到后台的实现方法_javascript技巧

给复选框命名:<input type='checkbox' name='checkTheme' value='"> 我在提交的时候onclick="aa();" function aa() { var bb = ""; var temp = ""; var a = document.getElementsByName("checkTheme"); for ( var i = 0; i < a.len

jquery是如何获取复选框的值

 获取复选框的值的方法有很多,举不胜举,在本文将为大家介绍下jquery是如何获取复选框的值的,感兴趣的朋友不要错过 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  <html>  <head>  <mce:style><!--  -->

jquery获取复选框的值的简单实例_jquery

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <mce:style><!-- --></mce:style><style mce_bogus="1"> </style>

简单php获取复选框的值代码

 代码如下 复制代码 <html> <head> <title>获取复选框的值</title> </head> <body> <form action="result.php" method="POST"> <input type="checkbox" name="year[]" value="1">1    &

jquery如何获取复选框的值_jquery

复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <mce:style><!-- --></mce:style><style mce_bogus="1"> &l

php获取复选框checkbox值内容实例

php要求同名表单元素用数组形式命名,并赋予value.否则他不能分解. 例1:利用foreach遍历checkbox  代码如下 复制代码 <input type=checkbox name="chk[]" value=1> <input type=checkbox name="chk[]" value=2> <input type=checkbox name="chk[]" value=3> 提交后,用$_

php获取复选框的值代码

因为复选框大多是几个同时存在的,我们都会name[]的名字必须是一个数组的 这样只要获取一个就可以了  代码如下 复制代码 html代码 <input type="checkbox" name="frm_tag[]" id="frm_tag" value="1">1 <input type="checkbox" name="frm_tag[]" id="frm

jquery获取复选框checkbox的值实现方法_jquery

jQuery API : each(callback)::以每一个匹配的元素作为上下文来执行一个函数. :checked :匹配所有选中的被选中元素(复选框.单选框等,不包括select中的option) js: //js获取复选框值 var obj = document.getElementsByName("interest");//选择所有name="interest"的对象,返回数组 var s='';//如果这样定义var s;变量s中会默认被赋个null值

php简单获取复选框值的方法_php技巧

本文实例讲述了php简单获取复选框值的方法.分享给大家供大家参考,具体如下: html: <form id="form1" name="form1" method="post" action="checkbox.php"> <input type=checkbox name=checkbox[] value="1"> <input type=checkbox name=chec