Solved

retrive text data

Posted on 2007-12-03
10
333 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

706 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

21 Experts available now in Live!

Get 1:1 Help Now