Submitting a HTML form dynamically

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vijaykumar8686
    New Member
    • Mar 2010
    • 3

    Submitting a HTML form dynamically

    Hi,
    I need to submit a HTML form dynamically. I have tried few ways of doing the same, but the values of the dynamically generated form elements were not retrieved upon form submission. Below is the code for client and server side. Kindly rectify my error. Thanks.

    Client Side:
    Code:
    <script type="text/javascript">
    var count = 1;
    function addRow(tableID){	
    	count++;
    	document.new_contact_form.count.value = count;
    	
    	var table = document.getElementById(tableID);
    
    	var rowCount = table.rows.length;
    	var row = table.insertRow(rowCount-1);
    	
    	var cell1 = row.insertCell(0);
    	var element1 = document.createElement("input");
    	element1.type = "checkbox";		
    	cell1.appendChild(element1);
    
    	var cell2 = row.insertCell(1);
    	var element2 = document.createElement("input");
    	element2.type = "text";
    	element2.size = "30";
    	element2.name = "contact_name" + count;	
    	cell2.appendChild(element2);
    
    	var cell3 = row.insertCell(2);
    	var element3 = document.createElement("input");
    	element3.type = "text";
    	element3.size = "30";	
    	element3.name = "contact_number" + count;	
    	cell3.appendChild(element3);		
    }
    
    function deleteRow(tableID){	
    	try {
    		var table = document.getElementById(tableID);
    		var rowCount = table.rows.length;
    
    		count--;				
    		document.new_contact_form.count.value = count;
    			
    		for(var i=0; i<rowCount; i++) {
    			var row = table.rows[i];
    			var chkbox = row.cells[0].childNodes[0];
    			if(null != chkbox && true == chkbox.checked) {
    				table.deleteRow(i);
    				rowCount--;
    				i--;				
    			}
    		}
    	}catch(e) {
    		alert(e);
    	}
    }
    </script>
    Server Side: (A Servlet)
    Code:
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		response.setContentType("text/html");
    		PrintWriter out = response.getWriter();
    	
    		scount = request.getParameter("count");
    		count = Integer.parseInt(scount);		
    	
    		try {
    			Class.forName("oracle.jdbc.driver.OracleDriver");
    			con = DriverManager.getConnection("jdbc:oracle:thin:@system-name:port:ServiceName", "username", "password");
    			stmt = con.createStatement();			
    		} catch (Exception e) {			
    			e.printStackTrace();
    		}
    		
    		for(int i = 1; i <= count; i++){			
    			contactNames[i] = request.getParameter("contact_name"+i);    // DISPLAYS NULL POINTER EXEPTION HERE
    			contactNumbers[i] = request.getParameter("contact_number"+i);
    			try {
    				stmt.executeUpdate("insert into sms_contacts values('Test_User','"
    						+ contactNames[i] + "','" + contactNumbers[i] + "')");
    			} catch (SQLException e) {				
    				e.printStackTrace();
    			}
    		}
    		out.write("<b>Contact Details have been saved!</b>");
    	}
    Last edited by gits; Mar 20 '10, 01:05 PM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    i assume that you test with IE? then you cannot set the name attribute dynamically at runtime ... have a look here ...

    kind regards

    Comment

    • vijaykumar8686
      New Member
      • Mar 2010
      • 3

      #3
      Hi,
      I have tried in both IE and Mozilla. I still get the same error message - "Null Pointer Exception", while submitting the form.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        do you receive the expected parameters at the server?

        Comment

        • vijaykumar8686
          New Member
          • Mar 2010
          • 3

          #5
          No. That's the place where the exception is being shown. The server side script is unable to receive the parameters. Instead of some value getting passed, a "null" is getting passed. In the server side code above (posted earlier), the value is retrieved at lines 17 and 18. Exception is thrown here.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            are the dynamically added nodes child-elements of the form that you submit? and did you have a look at the link that i showed above?

            Comment

            Working...