Solved

retrive values for dynamic text input

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
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)

920 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

13 Experts available now in Live!

Get 1:1 Help Now