Solved

retrive values for dynamic text input

Posted on 2007-12-03
3
505 Views
Last Modified: 2008-02-01
hi,
the following code works as following: when user click"add item" button a text input will show and user write values for example"para1" the user can click "add item" for second time and another text input will show and user will write 2nd values in the 2nd text input for example "para2"..and so on
when user done from his parameter entries he will click "submit" button to send the values to the server

you will find in the code the both side (clien and server)
The problem is that the server can save the last parameter (last text input entries) for example (para2) ONLY , I want him to retrive all text inputs , he can retrive just the last one
What is the solution??

--Client side (test.jsp)----
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="java.sql.*,java.io.*,java.util.*,java.lang.*"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
--%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
 
   <html>
       
       <head>
         
<script>
var arrInput = new Array(0);
var arrInputValue = new Array(0);
 
function addInput() {
  arrInput.push(arrInput.length);
  arrInputValue.push("");
  display();
}
 
function display() {
  document.getElementById('parah').innerHTML="";
  for (intI=0;intI<arrInput.length;intI++) {
    document.getElementById('parah').appendChild(createInput(arrInput[intI], arrInputValue[intI]));
  }
}
 
function saveValue(intId,strValue) {
  arrInputValue[intId]=strValue;
  var text ="";
  for(var o in arrInputValue)text +='\n'+o+':'+arrInputValue[o]
  
   
   }
}  
 
function createInput(id,value) {
  var obj = document.createElement('input');
  obj.type='text' 
  obj.name='test' 
  obj.id='test'+ id 
  obj.onchange=function () { saveValue(this.id,this.value) } 
  return obj
}
 
function deleteInput() {
  if (arrInput.length > 0) { 
     arrInput.pop(); 
     arrInputValue.pop();
  }
  display(); 
}
</script>
</head>
<body>
   
<form method="post" action="test_action.jsp" >
<div id="parah"></div>
 
<input type="button" value="add item" onclick="addInput();" />
<input type="submit" value="submit"/>
</form>
</body>
 
 
   </html>
--------------------------
---server side (test_action.jsp) -----
<% try{
    String[] str=request.getParameterValues("test"); 
    
    for (int i=0; i<str.length ; i++){
  //String x=str[1];
    out.println(str[i]);
   }
        
    %>

Open in new window

0
Comment
Question by:master_programmer
3 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 20396961
You are not saving the values into the newly created objects. Update function createInput(id,value) to the following:

function createInput(id,value) {
  var obj = document.createElement('input');
  obj.value=value;
  obj.type='text'
  obj.name='test'
  obj.id='test'+ id
  obj.onchange=function () { saveValue(this.id,this.value) }
  return obj
}
0
 
LVL 9

Expert Comment

by:ysnky
ID: 20401221
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 20415300
Or from your function create input change the line

obj.id='test'+ id
 to

obj.id='test'


then your  getParamtervlues will provide you a array of all inputs.
The drawback of this approach is that you can not gaurntee the order in which you will recieve these inputs.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question