• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to change name of hidden text field dynamically  RSS feed

 
Samantha Simon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

On my JSP web page, I need to have a hidden text field. This field's name and value have to change each time a FOR loop is run. How do I change the name dynamically?

My text box looks like this, on load of the page:



Then, I have a loop, something like this:



Can anyone here help me figure out how to do this? I'm a beginner, so I don't know much about this stuff, so the simpler the better!

Thanks in advance. Any advice would be greatly appreciated...

Samantha
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this using Java or JavaScript? I'm suspecting the latter, but you don;t say. If so, why wait until after the page loads? Where is the information for the new names coming from?

You need to provide more details if you want help.
 
Samantha Simon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response.

The code I posted is in Java. The information for the name is coming from the database, and it is stored in the vector I showed in the code. I needed to find a way to dynamically name the hidden field each time the loop is run.

What I did was,
1. I created the hidden field (the one I called "TOBE_CHANGED" in my above post) in HTML, with the "name" attribute set to get the value of another hidden field called FIELD_NAME.
2. I created a second hidden field called FIELD_NAME.
Then, in the FOR loop,
3. I gave FIELD_NAME a value, from the DB. (The same value which needs to be the "name" of the first hidden field.) This value, in my code which I posted above, is r.get(7).toString.
4. I gave the first hidden field (again, I'm referring to the field I called "TOBE_CHANGED" in my previous post) a new value from the DB. This value, in my code which I posted above, is r.get(4).toString().

This gave me the result I wanted. I now have my FOR loop assigning a different name and a different value to my first hidden field, each time the loop is run.

I'd still love to hear any comments on this, or if I should have done this differently.

Thank you very much!

Samantha
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of things.

First of all, Java in modern JSP is no longer acceptable. You should be using the JSTL and EL to establish dynamic elements in your JSP. So rather than a Java for-loop, you'd use the JSTL's <c:forEach> tag. If you are a beginner, it's important to start off on the right foot, and using Java scriptlets is a poor practice that's been discredited for almost 10 years now. You are heading down the wrong path to JSP knowledge.

Use good variable naming. "vect" and "r" are very poor choices.

Use List rather than Vector.

OK, on to your specific problem. Let's say that the page controller gathers the names for the hidden fields from the database and puts them into a scoped variable named hiddenNames (much better than vect, no?). On the page you can create the hidden input elements with something along the lines of:
 
Samantha Simon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, Java in modern JSP is no longer acceptable. You should be using the JSTL and EL to establish dynamic elements in your JSP. So rather than a Java for-loop, you'd use the JSTL's <c:forEach> tag. If you are a beginner, it's important to start off on the right foot, and using Java scriptlets is a poor practice that's been discredited for almost 10 years now. You are heading down the wrong path to JSP knowledge.


1. Thanks for the tip. I've been working on an old website and making some pretty minor changes - I'm not creating it. This website was written with Java in JSP. Is it hard to learn JSTL? Do you know of a clear tutorial teaching how to use it? As a beginner, I'd be interested in learning it.

Use List rather than Vector.

2. How come list is more appropriate than vector?

3. Since I won't be using JSTL or EL here, does your suggestion still apply?

Thanks again,

Samantha
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samantha Simon wrote:
1. Thanks for the tip. I've been working on an old website and making some pretty minor changes - I'm not creating it. This website was written with Java in JSP.

I see. My condolences. Just be aware that scriptlets are no longer viable and should never be used in new code.

Is it hard to learn JSTL? Do you know of a clear tutorial teaching how to use it? As a beginner, I'd be interested in learning it.

You very much should learn it as it is the way that moderne JSP should be written. Not very hard. Most moderne tutorials and books should focus on JSTL and EL. And the Specification is an easy read.

2. How come list is more appropriate than vector?

Since Java 1.2 List is preferred over Vector. It's also more well-behaved in the JSP environment. If the code is already using Vector successfully, probably not worth changing, but for new code, a List implementation such as ArrayList is customary.

3. Since I won't be using JSTL or EL here, does your suggestion still apply?

Sort of, except you'd need to use a scriptlet for-loop and scriptlet expression. But the point of creating the hidden elements inside the loop doesn't change, and using better variable names is always in vogue.
 
Samantha Simon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response.

Wouldn't your suggestion create a new hidden element each time the for loop was looped through?

I specifically needed to have only one hidden element, and needed it's name to change each time. I did get this result the way I did it, with the second hidden element storing the name.

Samantha

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samantha Simon wrote:Wouldn't your suggestion create a new hidden element each time the for loop was looped through?

Yes.

I specifically needed to have only one hidden element, and needed it's name to change each time.

That makes no sense to me. What would be the purpose of this? As only the last assigned name would have any meaning.

 
Samantha Simon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for having only one input field, and switching it's name in each iteration of the loop is exactly what you said: So that only the last one has any meaning. I only want one input field.

The reason I only want one, and want to give it a different name each time the loop is run, is as follows:

- I have some functions (functions that have been built and running for years now) which add products to the shopping cart of this website.
- These functions require that for each item being added to the cart, there be a hidden field with a name and value specific to that item.
- I have created a new additional page for this website. This page calls the above mentioned functions, in order to add more than one item to the cart, using a loop.
- Since I am adding a different item to the cart with each iteration of the loop, I need to name the input field differently every time the loop is looped through.

(I know, it's strange, but that's how this website works. It's not really set up to add more than one item to the cart per page, which is what I am doing here.)

Does this make more sense now?

Thanks for the help,

Samantha

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it still makes no sense at all. If you only have one element, then giving it more than one name in a loop is completely non-sensical. When it comes time to submit the form containing the element, only the last name assigned is used, so what's the use of the other names?
 
Samantha Simon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When it comes time to submit the form containing the element, only the last name assigned is used, so what's the use of the other names?


I don't need to use this field after the form is submitted. I use the the field within the loop, before submitting the form. It is WITHIN the same loop that (1)the field is assigned a new name/value, and (2)that the functions are called to add the item to the cart.
And those are the functions which need a specific name/value.

Do you still think this wouldn't work for adding more than one item to the cart?

(Thanks for the prompt reply!)
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hidden inputs exist for submission. It makes no sense to use them for temporary values. So no, still not making sense.

If you need temporary values, why aren't you just using variables?
 
Samantha Simon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could use a variable instead of the hidden field I called FIELD_NAME.
But the functions I need to use to add the items to the cart need specifically a hidden field with a new name and value each time they are called.

Are you saying that the hidden field will not hold any value before the form is submitted? Will I have to submit the form each time the name and value of the field change in the loop?

(as you can tell, I'm very new to web development...)
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, doing anything to a form input element without submitting will have no effect at all. Were you expecting that just setting a name and a value into the element would cause something to happen on the server? It will not.

In fact, I'm questioning why any of this is happening in a JSP at all. JSPs are intended to merely create an HTML file to send to the browser. All processing should happen in a servlet when the HTML form is submitted.

You are really going to have to step back and take the time to understand how HTTP works, and how web apps are structured, and how JSP fits into the picture.

I suggest, at minimum, that you read through the following articles:
  • The Secret Life of JSPs
  • The Front Man

  •  
    Samantha Simon
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, doing anything to a form input element without submitting will have no effect at all. Were you expecting that just setting a name and a value into the element would cause something to happen on the server? It will not.


    This website has a class which tracks the session parameters. Every time I use this class's function to set a field, the name of the field and its value are put into a hashmap. Every time I use this class's function to get a field's value, it gets the value from the hashmap.

    This being the case, does the fact that the field name and value will not change on the server matter in my case?
    The name and value will be changed in the hashmap, and will be retrieved from there. I don't think this hidden field will involve the server at all until I submit the page - but do I care about that? I'm very interested in hearing your opinion about this.

    Thanks for the links to those articles. I will definitely read up on this topic.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!