• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

creating text fields dynamically


I am creating hidden fields and  text fields dynamically through a javascript function as below .
      
      count=0;
      function addAssistant() {
      count=count+1;
      ............
      strReplace +="<td><input type=\"text\" value=\"\" name=\"assistant"+count +"\" id=\"assistant"+count +"\"></td>";
      strReplace +="<td><input type=\"hidden\" value=\"\" name=\"assistanthid"+count+"\" id=\"assistanthid"+count+"\"></td>";

      ..........
      divTable.innerHTML=strReplace;
      .......
      }


      When I submit the form ( // I have a submit  button )  , How do I get these hidden field's values in the Servlet code ?

      To be more specific ,my problem is I don't know how many such hidden text fields user will create and submit . So, How do I get those multiple hidden  field values in the servlet ?

What are the easier ways to solve this problem ?
0
cofactor
Asked:
cofactor
  • 6
  • 3
  • 3
  • +2
1 Solution
 
Gurvinder Pal SinghCommented:
are these text boxes created inside a form?
If not, then these values will not be submitted on the form submit.

0
 
cofactorAuthor Commented:
>>>are these text boxes created inside a form?

yes.

note , i also have another function which does  deleteRow(this) .

So, user can create/delete text fields .

My problem is , how do I get the values in the servlet code ?

0
 
Gurvinder Pal SinghCommented:
Are you not able to get these values using request.getParameter('elementName')?

If not, can you please share the html/jsp and servlet code?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
cofactorAuthor Commented:
>>>Are you not able to get these values using request.getParameter('elementName')?

Thats exactly my problem !

I can't do that , because user is creating elements dynamically .

see the posted code again ,

  strReplace +="<td><input type=\"hidden\" value=\"\" name=\"assistanthid"+count +"\" id=\"assistanthid"+count +"\"></td>";

look , there is a count .



Now  tell me , how do i use   request.getParameter()  to find all those dynamic hidden field values ?
0
 
Gurvinder Pal SinghCommented:
being dynamic and having a count is not a problem as long the fields are inside the form and they have a valid and unique name.

Please attach your html/jsp for more clarity
0
 
geertplaisierCommented:
My suggestion is to add an hidden field which contains the count variable.

so add
<input type="hidden" id="count" name="count" value="" />

and add to your function
document.getElementById('count').value = count

just before you replace the innerHTML code of the DIV element.

In your servlet you can use the count field to get all the other fields, because then you know
how many fields there are and you can derive their names
0
 
cofactorAuthor Commented:
>>>being dynamic and having a count is not a problem as long the fields are inside the form and they have a valid and unique name.

you are not getting my point.

I can't write  this way ....because there could be more number of  fields created by the user .

request.getParameter('assistanthid1')
request.getParameter('assistanthid2')
request.getParameter('assistanthid3')
request.getParameter('assistanthid4')



0
 
cofactorAuthor Commented:
>>>so add
<input type="hidden" id="count" name="count" value="" />

>>and add to your function
>>document.getElementById('count').value = count


good ...but this also will not work ...because I am using deleteRow function which can be used by user and can be deleted ...so count will change .
0
 
geertplaisierCommented:
Then you should update the count value in the deleteRow function again.
So in that function, again use: document.getElementById('count').value = count

The in the servlet you can use:

for(int i = 0; i < request.getParameter('count'); i++) {
    request.getParameter('assistanthid'+i);
    // and then you can do something with that result
}
0
 
cofactorAuthor Commented:
>>>Then you should update the count value in the deleteRow function again.
So in that function, again use: document.getElementById('count').value = count

fine ....but this also will not work.

because, suppose user created  textfield 1 , textfield 2 , textfield 3 dynamically.

Now he deletes textfield2 .

So, now we will have fields as  textfield 1 ,  textfield 3 .

If I use your method then then I can't find the values in the server

0
 
geertplaisierCommented:
You could deal with this serverside.
Then you should not update the count field inside the deleteRow function.
On the server use the function:

for(int i = 0; i < request.getParameter('count'); i++) {
    if(request.getParameter('assistanthid'+i)!=null) {
         // and then you can do something with that result
    }
}

Or you could do it client-side.
In that case you should store not the total count, but each count,
for example comma-seperated:

<input type="hidden" id="count" name="count" value="" />

and in your addAssistant function

document.getElementById('count').value += count + ',';

and in your deleteRow funciton use:

document.getElementById('count').value = document.getElementById('count').value.replace(count + ',', '');

The in your servlet, use the count value, remove the comma's and you know which
fields you can have.
0
 
rrzCommented:
Please try the following code.
<%@ page import="java.util.Enumeration" %>
<%
  
  for(Enumeration<String> names = request.getParameterNames(); names.hasMoreElements();){
            String name = names.nextElement();  
            if(name.startsWith("assistanthid))out.print(name + " = " + request.getParameter(name) + "<br/>");
  }
%>

Open in new window

0
 
rrzCommented:
error above
<%@ page import="java.util.Enumeration" %>
<%
  for(Enumeration<String> names = request.getParameterNames(); names.hasMoreElements();){
            String name = names.nextElement();  
            if(name.startsWith("assistanthid"))out.print(name + " = " + request.getParameter(name) + "<br/>");
  }
%>

Open in new window

0
 
josephtsangCommented:
But is it necessary to name those hidden fields as "assistant1", "assistant2"... etc.?

I have a suggestion. It was a work around before in a project I participated:

Firstly, just name those hidden fields simply as "assistant".

Now, create 2 redundant hidden fields right in front of your code (of course, also named it as "assistant"), such that if there are always at least 2 such hidden fields even if the user doesn't create any.

Because we can guarantee the hidden fields are always more than 1 when they come to the server side, the way you use to call these values should be request.getParameterValues("assistant"), and it will return you an array of String.

To locate a specific value, just minus 2 from the index. To count how many fields the user has created, just check the length of the array and minus it by 2.
0
 
cofactorAuthor Commented:
not tested .  but looks like a convincing code  ... hence clearing it up.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now