Solved

retrive values for dynamic text input

Posted on 2007-12-03
3
506 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

730 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