Solved

retrive text data

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

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

 
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

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

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

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

16 Experts available now in Live!

Get 1:1 Help Now