简体   繁体   中英

Inserting HTML DOM values into the database

I have created a button named 'Add Textbox' using HTML which on clicked would create a textbox. The variable name of the text box created is '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. Thus the variable name of every text box is 'textid + addRowIndex'. I would like to insert these textbox values into the mysql database. 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.

PS: I suppose that the 'request.getParameter()' in the JSP Code is not written correctly. And what code should be added in JSP so as to enter the values of subsequent textboxes into the databases.

HTML CODE:

<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:

<%
        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. Then you can map a url (mydomain/upload?row=mydata) and fetch the data from your row input box.

*Or you can use jsp form elements to add indexed rows to form and on hitting submit button all the rows will be uploaded. Refer this for using spring MVC and uploading form data. 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. you can find reference here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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