Solved

retrive values for dynamic text input

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
This video teaches viewers about errors in exception handling.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now