Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

retrive text data

Posted on 2007-12-03
10
Medium Priority
?
344 Views
Last Modified: 2010-04-01
using the following code which is : have a button each time the button click it generate input of type text
THE QUESTION: HOW I CAN RETRIVE THE VALUES ON INPUTS TEXT

In server side i used :
String[] temp =  request.getParameterValues("test");
when i tried to print temp[i], it is null
what is the problem ?

<head>
<div id="parah"></div>
<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').innerHTML+=createInput(arrInput[intI], arrInputValue[intI]);
  }
}
 
function saveValue(intId,strValue) {
  arrInputValue[intId]=strValue;
}  
 
function createInput(id,value) {
  return "<input type='text'name='test' id='test "+ id +"' onChange='javascript:saveValue("+ id +",this.value)' value='"+ 
 
value +"'><br>";
}
 
function deleteInput() {
  if (arrInput.length > 0) { 
     arrInput.pop(); 
     arrInputValue.pop();
  }
  display(); 
}
</script>
</head>
<body>
<form method="post" action="test.jsp">
<input type="button" value="add item" onclick="addInput();" />
<input type="submit" value="submit"/>
</form>
</body>

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
10 Comments
 
LVL 23

Expert Comment

by:cmalakar
ID: 20395859
no space before name attribute..

type='text'name='test' ----> type='text' name='test'
0
 

Author Comment

by:master_programmer
ID: 20395918
not this the problem
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20395919
1. you are adding it to a div, not to the form
2. you could use createElement and appendChild instead:


<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;
}  
 
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.jsp" onSubmit="alert(this.elements[0].name); return false">
<div id="parah"></div>
 
<input type="button" value="add item" onclick="addInput();" />
<input type="submit" value="submit"/>
</form>
</body>

Open in new window

0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

Author Comment

by:master_programmer
ID: 20396100
thanx mplungjan, it works BUT IT STORE THE LAST VALUE ENTERED IN THE INPUT TEXT, I WANT IT
TO STORE ALL VALUES , EACH TIME THE INPUT TEXT APPEAR
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20396119
No need to shout. I can read you fine from here
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 1500 total points
ID: 20396179
Here is a debugged version
<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]
  alert(text)
}  
 
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.jsp" onSubmit="alert(this.elements[0].name); return false">
<div id="parah"></div>
 
<input type="button" value="add item" onclick="addInput();" />
<input type="submit" value="submit"/>
</form>
</body>

Open in new window

0
 

Author Comment

by:master_programmer
ID: 20396351
nothing changed, how the calling will be in server side?
my application should present more than one text input, each time the click "add item" button,
but when the user click the submit button the values written in each text input must saved in array?
0
 
LVL 9

Expert Comment

by:ysnky
ID: 20397711
you need to move <div id="parah"></div> into <form></form> area, and use the first aproach you used.


btw this is the doubled question with:
http://www.experts-exchange.com/Programming/Languages/Java/Q__22997226.html
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20398102
That is what I said. Move the div
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20401322
And the "B"  grade was for ???
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article discusses how to implement server side field validation and display customized error messages to the client.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

721 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