在数据库(JSP PAGE)中获取多个复选框结果
Getting multiple checkbox results in Database (JSP PAGE)
我需要让用户给第一页一些数据,比如(城市、国家、国家/地区的 URL),我需要他选择什么样的目的地:
冬天,圣诞节,夏天(他可以检查最多2个)
如何将结果与我的数据库连接起来?
我创建了 3 个表
- 带列节点 (PK), 城市, 国家, 网址
- nodest_c(包),类别 nodest (fk --
- > nodest), nodest_c (fk --> nodest_c)
创建目标代码.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>create dest</title>
</head>
<body>
<html>
<head>
<title>CREATE DESTINATION</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1> CREATING DESTINATION </h1>
<form name="createdest" method="get" action="create dest code.jsp">
Country: <input type="text" required name="id8" /> <br>
City: <input type="text" required name="id9" /> <br>
URL Video: <input type="url" required name="id10" /> <br> <br>
<i><ins>Categorize the destination (max 2): </ins></i> <br> <br>
<input type="checkbox" name="id5" value="1" onClick="return KeepCount()" >Christmas<br>
<input type="checkbox" name="id6" value="2" onClick="return KeepCount()" >Winter <br>
<input type="checkbox" name="id7" value="3" onClick="return KeepCount()" >Summer <br> <br>
<input type="submit" value="CREATE DESTINATION" />
<br>
</form>
<SCRIPT LANGUAGE="javascript">
function KeepCount() {
var NewCount = 0
if (document.createdest.id5.checked)
{NewCount = NewCount + 1}
if (document.createdest.id6.checked)
{NewCount = NewCount + 1}
if (document.createdest.id7.checked)
{NewCount = NewCount + 1}
if (NewCount == 3)
{
alert('Pick Just Two Please')
document.createdest; return false;
}
}
</SCRIPT>
</body>
</html>
插入数据库.jsp
<%@page import="java.sql.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>create dest code</title>
</head>
<body>
<%
int m[] = new int[5000]; String s[] = new String[5000];
Class.forName("com.mysql.jdbc.Driver");
String myDatabase = "jdbc:mysql://localhost:3306/project_app?user=root&password=1234";
Connection myConnection = DriverManager.getConnection(myDatabase);
Statement myStatement = myConnection.createStatement();
boolean check=null!=request.getParameter("id5");
boolean check1=null!=request.getParameter("id6");
boolean check2=null!=request.getParameter("id7");
String id8=request.getParameter("id8");
String id9=request.getParameter("id9");
String id10=request.getParameter("id10");
String sqlString = "INSERT INTO DEST(COUNTRY,CITY,URL) VALUES ('"+id8+"', '"+id9+"','"+id10+"')";
myStatement.executeUpdate(sqlString);
myStatement.executeUpdate(sqlString1);
myStatement.close();
myConnection.close(); %>
</body>
<h1 style="color:blue;">Successful Registration </h1>
</html>
有什么建议吗?
在你的
servlet中使用以下代码:
String checked = request.getParameterValue("checkboxName");
如果选中为 null,则表示您的复选框未选中。
另外,如果您想使用复选框组,请放置一些具有相同名称的复选框并使用它:
String[] checkedValues = request.getParameterValues("checkboxName");
然后,选中的所有复选框值都将添加到checkedValue变量中。
boolean check = null != request.getParameter("id5");
boolean check1 = null != request.getParameter("id6");
boolean check2 = null != request.getParameter("id7");
根据您在数据库中用于检查列的数据类型,您可以使用这些布尔值。
相关文章:
- 使用springform时,如何在ajax/jquery中获取复选框值
- 使用 javascript 获取复选框的状态
- 从圣杯控制器获取复选框值
- 如何使用 JavaScript 获取复选框项的值
- 如何获取复选框值并将其插入到挖空中的可观察数组中
- <JSP>正在获取复选框的值
- 在表单提交中获取复选框值,并使用jQuery将其存储在变量中
- 获取复选框值选中-JavaScript
- Javascript - 如何获取复选框组的总和
- 如何在jquery中获取复选框选定的对象
- Javascript - 从表单中获取复选框值并获取随机结果
- 使用 jQuery 获取复选框的值为 1/0
- 如何在 Jquery 中获取复选框的 ID
- JQUERY ON('change') 获取复选框值和状态
- 如何获取复选框的文本
- 如何获取复选框值而不是所选选项
- 如何从HTML表中的多个单元格获取复选框
- 如何在 MVC-4 中的编辑模式弹出窗口中获取复选框
- Jquery,如何获取复选框未选中的事件和复选框值
- 使用 jquery 从 javascript 树中获取复选框值