Solved

retrive text data

Posted on 2007-12-03
10
341 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
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
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.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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 …

828 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