?
Solved

retrive values for dynamic text input

Posted on 2007-12-03
3
Medium Priority
?
509 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 1500 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month9 days, 18 hours left to enroll

762 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