Solved

retrive text data

Posted on 2007-12-03
10
342 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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 500 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allow link tag to be submit like form 15 60
Where is this content coming from? 4 40
Add Rows on a Table 8 53
Global version website variable for clearing cache 10 30
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…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

739 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