简体   繁体   English

将HTML DOM值插入数据库

[英]Inserting HTML DOM values into the database

I have created a button named 'Add Textbox' using HTML which on clicked would create a textbox. 我使用HTML创建了一个名为“添加文本框”的按钮,单击该按钮将创建一个文本框。 The variable name of the text box created is 'textid'. 创建的文本框的变量名称为“ textid”。 Now since no two textboxes could have the same variable name, I have created a variable, named 'addRowIndex' and assigned it to the variable name of every textbox. 现在,由于没有两个文本框可以具有相同的变量名,因此我创建了一个名为“ addRowIndex”的变量,并将其分配给每个文本框的变量名。 Thus the variable name of every text box is 'textid + addRowIndex'. 因此,每个文本框的变量名称为“ textid + addRowIndex”。 I would like to insert these textbox values into the mysql database. 我想将这些文本框值插入mysql数据库。 But instead when I enter the values and click submit, a null value is entered instead of the entered textbox value. 但是,当我输入值并单击提交时,输入的是空值而不是输入的文本框值。 Also I am unable to insert the subsequent textbox values into the database. 另外,我无法将后续的文本框值插入数据库。 Below are the following code for HTML, Javascript and JSP that I have used for the complete process. 以下是用于完整过程的以下HTML,Javascript和JSP代码。

PS: I suppose that the 'request.getParameter()' in the JSP Code is not written correctly. PS:我想JSP代码中的'request.getParameter()'编写不正确。 And what code should be added in JSP so as to enter the values of subsequent textboxes into the databases. 以及应在JSP中添加什么代码,以便将后续文本框的值输入数据库。

HTML CODE: HTML代码:

<html>
  <head>
  </head>
  <body>
    <button type="button" onclick="addRow()">Add Textbox</button>
  </body>
</html>

JAVACRIPT CODE: 验证码:

<script>
  var addRowIndexArray = [];
  function addRow() {
    var addRowIndex = addRowIndexArray.length;
    var createDiv = document.createElement("div");
    createDiv.id = "innerDiv";
    addRowIndexArray.push(document.body.appendChild(createDiv));

    var createText = document.createElement("input");
    createText.type = "text";
    createText.id = "textid";
    createText.name = "textid" + addRowIndex;
    createDiv.appendChild(createText);

    var createBr = document.createElement("br");
    createDiv.appendChild(createBr);
  };
</script>

JSP CODE: JSP代码:

<%
        PreparedStatement ps = null;
        ResultSet rs = null;

        String driverName = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/connection";
        String user = "root";
        String password = "nike";

        String textBox = request.getParameter("textid + addRowIndex");

        String sql1 = "insert into demo1 values('" + textBox + "')";

        try {
            Class.forName(driverName);
            java.sql.Connection con = DriverManager.getConnection(url, user, password);
            ps = con.prepareStatement(sql1);
            Statement stmt = con.createStatement();
            stmt.executeUpdate(sql1);
            rs = ps.executeQuery();
            con.close();

        } catch (Exception e) {
            System.err.println("Got an exception!");
            System.err.println(e.getMessage());
        }


    %>

I don't think the way you are getting the request object and parameter from it is right.The right way would be to configure request handler using servlets or spring controllers. 我认为从中获取请求对象和参数的方法不正确,正确的方法是使用servlet或spring控制器配置请求处理程序。 Then you can map a url (mydomain/upload?row=mydata) and fetch the data from your row input box. 然后,您可以映射一个URL(mydomain / upload?row = mydata)并从行输入框中获取数据。

*Or you can use jsp form elements to add indexed rows to form and on hitting submit button all the rows will be uploaded. *或者您可以使用jsp表单元素向表单添加索引行,然后单击“提交”按钮,所有行都将被上传。 Refer this for using spring MVC and uploading form data. 有关使用spring MVC和上传表单数据的信息,请参考此内容。 http://www.mkyong.com/spring-mvc/spring-mvc-file-upload-example/ http://www.mkyong.com/spring-mvc/spring-mvc-file-upload-example/

*In case you have to upload rows on addRow button click asynchronously you should consider using ajax/xhr for doing so. *如果必须通过addRow按钮上载行以异步方式单击,则应考虑使用ajax / xhr进行操作。 you can find reference here 你可以在这里找到参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM