表单中输入的数据不会发送到Oracle数据库

时间:2015-12-08 07:50:23

标签: javascript html database twitter-bootstrap jsp

我正在尝试在Bootstrap中创建注册表单,然后将其连接到Oracle数据库,但用户输入的数据不会向数据库发送任何值。请建议我应该做什么编辑。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Home - Student Registration Form</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/datepicker.css" rel="stylesheet">
    <style>
      .error{
        color: red;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="row">
        <form id="regform" class="form-horizontal" action="NewFile.jsp">
          <fieldset>
          <!-- Form Name -->
          <legend>Student Registration</legend>
          <!-- Text input-->
          <div class="form-group">
            <label class="col-md-4 control-label" for="firstname">First Name</label>
            <div class="col-md-4">
            <input id="firstname" name="firstname" type="text" placeholder="First Name" class="form-control input-md">
            </div>
          </div>
          <!-- Text input-->
          <div class="form-group">
            <label class="col-md-4 control-label" for="lastname">Last Name</label>
            <div class="col-md-4">
            <input id="lastname" name="lastname" type="text" placeholder="Last Name" class="form-control input-md">
            </div>
          </div>
          <!-- Text input-->
          <div class="form-group">
            <label class="col-md-4 control-label" for="dob">Date of birth</label>
            <div class="col-md-4">
            <input id="dob" name="dob" type="text" placeholder="Date of birth" class="form-control input-md">
            </div>
          </div>
          <!-- Text input-->
          <div class="form-group">
            <label class="col-md-4 control-label" for="age">Age</label>
            <div class="col-md-4">
            <input id="age" name="age" type="text" placeholder="Age" class="form-control input-md" disabled>
            </div>
          </div>
          <div class="form-group">
            <label class="col-md-4 control-label" for="sex">Sex</label>
            <div class="col-md-4">
               <label class="radio-inline"><input type="radio" name="sex" value="Male">Male</label>
          <label class="radio-inline"><input type="radio" name="sex" value="Female">Female</label>
            </div>
          </div>
          <!-- Select Basic -->
          <div class="form-group">
            <label class="col-md-4 control-label" for="subjects">Subjects</label>
            <div class="col-md-4">
              <select id="subjects" name="subjects" class="form-control">
                <option value="1">Database</option>
                <option value="2">ADA</option>
                <option value="3">Networking</option>
              </select>
            </div>
          </div>
          <!-- Textarea -->
          <div class="form-group">
            <label class="col-md-4 control-label" for="localaddress">Local Address</label>
            <div class="col-md-4">
              <textarea class="form-control" id="localaddress" name="localaddress"></textarea>
            </div>
          </div>
          <!-- Multiple Checkboxes (inline) -->
          <div class="form-group">
            <label class="col-md-4 control-label" for="localaddresscheckbox">Permenant address</label>
            <div class="col-md-4">
              <label class="checkbox-inline" for="localaddresscheckbox">
                <input type="checkbox" name="localaddresscheckbox" id="localaddresscheckbox" value="1">
                Copy Local Address to permanent Address
              </label>
            </div>
          </div>
          <!-- Textarea -->
          <div class="form-group">
            <label class="col-md-4 control-label" for="permenantaddress">Permenant Address</label>
            <div class="col-md-4">
              <textarea class="form-control" id="permenantaddress" name="permenantaddress"></textarea>
            </div>
          </div>
          <!-- Button -->
          <div class="form-group">
            <label class="col-md-4 control-label" for="submit"></label>
            <div class="col-md-4">
              <button id="submit" name="submit" class="btn btn-primary">Submit</button>
            </div>
          </div>
        </fieldset>
        </form>
      </div>
    </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
    <script src="js/bootstrap-datepicker.js"></script>
    <script src="js/jquery.validate.min.js"></script>
    <script>
      (function() {

        jQuery.validator.addMethod("lettersonly", function(value, element) {
          return this.optional(element) || /^[a-z]+$/i.test(value);
        }, "Letters only please");

        $("#regform").validate({
          rules: {
            dob: "required",
            localaddress: "required",
            permenantaddress: "required",
            firstname: {
                lettersonly: true,
                required: true
            },
            lastname: {
                lettersonly: true,
                required: true
            }
          },
          submitHandler: function(form) {
            form.submit();
          }
        });

        //init datepicker
        $('#dob').datepicker({
            'format': 'yyyy-mm-dd',
            'autoclose': true
        });

        //copy localaddress to permenant address on checkbox click
        $('#localaddresscheckbox').click(function(){
          if($(this).is(':checked')) {
            var localaddress = $('#localaddress').val();
            $('#permenantaddress').val(localaddress); //copy local address to permenant address box
          }
          else {
            $('#permenantaddress').val('');
          }
        });

        //age handler
        $('#dob').datepicker().on('changeDate', function (ev) {
          //get current date
          var today = new Date();
          var currentYear = today.getFullYear(); //current year
          var selectedYear = $(this).val().split('-')[0]; //selected dob year
          var age = Number(currentYear) - Number(selectedYear);
          $('#age').val(age);
        });

      })();
    </script>
  </body>
</html>

现在,本地地址的数据不会被复制到永久地址。

服务器端代码如下:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String firstname=request.getParameter("firstname");
System.out.println("First name="+firstname);
String lastname=request.getParameter("lastname");
System.out.println("Last name="+lastname);
String dob=request.getParameter("dob");
System.out.println("Date of Birth="+dob);
String age=request.getParameter("age");
System.out.println("Age="+age);
String sex=request.getParameter("sex");
System.out.println("Sex="+sex);
String subjects=request.getParameter("subjects");
System.out.println("Subject="+subjects);
String localaddresscheckbox=request.getParameter("localaddresscheckbox");
System.out.println("Local Address="+localaddresscheckbox);
String permanentaddress=request.getParameter("permanentaddress");
System.out.println("Permanent Address="+permanentaddress);

try
{
//System.out.println(0);
//Class.forName("oracle.jdbc.driver.OracleDriver");

Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println(1);
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","Subhankar","oracle07");  
System.out.println(2);

Statement st= con.createStatement();
//String sql = "INSERT INTO STUDENT_DETAILS VALUES (firstname,lastname,dob,age,sex,subjects,localaddresscheckbox,permanentaddress)";

String sql="INSERT INTO STUDENT_DETAILS (first_name,last_name,date_of_birth,age,sex,subject,local_address,permanent_address) VALUES ('"+firstname+"','"+lastname+"','"+dob+"','"+age+"','"+sex+"','"+subjects+"','"+localaddresscheckbox+"','"+permanentaddress+"')";
st.executeUpdate(sql);


//
//ResultSet rs=st.executeQuery("select * from studentdetails where studentid="+studentId);

}catch(Exception e)
{
    e.printStackTrace();
}

%>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您尚未设置操作属性。
如果您想将表单提交到同一页面使用

action="<?php echo $_SERVER['REQUEST_URI'] ?>"