• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamic Form

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going to be making a JSP page that is just a big form for collecting information. What I need though is to allow additional form fields to be displayed as necessary.
For example, I am requesting Author information for periodicals which may or may not have more than 1 author. I don't really want to require the user to type them all on one input field and then me have to parse them out.
So I need to have a field and then a checkbox or something that when chosen, adds another Author Text Field right below the original one, and so on...
Is this possible? Any information regarding this would be great.
Thanks.
 
Sam Anjos
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gregg,
To my knowledge it can't be done with JSP. I'm actually working on somethig similar and what I used was JavaScript.
Here's the code that I used:
function insertNewRow()
{
// code for netscape
var t = document.getElementById('originalRow'-row id name).cloneNode(true);
document.getElementById('cloneMe'-table id name).appendChild(t);
}
Unfortunetly this code only works on netscape, and I also haven't figured out how to get the code into a jsp page after.
Sorry I couldn't help you with more.
Sam A.
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should get you started,

don't foget to change "on_click" to "onclick"
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the lame post before, didn't read it all the way thru before I thought I new what you wanted.

Don't forget the on_click change.
 
Sal DiStefano
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicely done John!!
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the input. I will give it a shot tonight and see what happens.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, that is perfect!!! One last question though. Should I just use a counter in the Javascript function and append a number to the name of each Author Input Text so that I can retrieve each one in the called Servlet?
Thanks again. You da man!
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
One last question though. Should I just use a counter in the Javascript function and append a number to the name of each Author Input Text so that I can retrieve each one in the called Servlet?

That would be my recommendation.
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
Should I just use a counter in the Javascript function and append a number to the name of each Author Input Text so that I can retrieve each one in the called Servlet?

With the counter approach you could add a hidden field and increment the count with each additional field and then when you get the data back to the server you read it first and then you'd know how many they added. That way if they added 5 authors and then went back and blanked out the third guy you wouldn't misread that as a field that didn't exist and stop your processing.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Hembree:

With the counter approach you could add a hidden field and increment the count with each additional field and then when you get the data back to the server you read it first and then you'd know how many they added. That way if they added 5 authors and then went back and blanked out the third guy you wouldn't misread that as a field that didn't exist and stop your processing.

I like that idea. And since you brought it up, how would I remove one of the fields? I am probably going to replace the Checkbox with an ImageLink or button of some sort. So I would have a Button that says Add and a Button that says remove.
Sorry, but I know very little about Javascript. If you could provide a good link to this information, that would be good enough.
Thanks.
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Removing one is going to be a lot harder than adding one. You'll have to search for the name you want to remove and then count back to the beginning of the string and then the next occurence of the ">" after your starting position, since the name field would be different and the value field could be anything. This would be a nice feature but what if the user doesn't clean up his work, you'll need to do this back at the server for them.
As for a Javascript site, I can't recommend any really, I bought a book from O'Reilly on
Javascript and the Pocket Reference. After that anytime I need sonmething I just go to yahoo and search on javascript and whatever I'm looking for. I don't use it that often and their is a lot of stuff out there on the web for it, including what the other post was referring to when they said it was a netscape only solution. The browsers out there refer to the form fields differently and depending on your supported platforms you'll need to add the appropriate code to make it work right.
Here's a couple of sites that might get you going though.
http://www.javascript.com and http://webreference.com/programming/javascript/
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I got the adding and removing of the textfields working really good. Now I have another problem. When I add more than 1 text field, and then I do a view source on the html, I don't see the extra textfields in the html. So when I request those param from my servlet, they just end up with a null value. Any clue why this would happen?
To see this in action go to http://gregg.embeddedthought.com/AddReference.html
Thanks.
[ August 02, 2003: Message edited by: Gregg Bolinger ]
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, "view source" is going to show you the original page sent down by the server in your temporary internet file directory, where all internet files are cached locally. It won't know/show what the page has become.
In order to check that it would originally work I made the form with method="get" and then when I clicked on submit this posted all the form values on the query string(url), so if you do the same simple test you should be able to see it. Once you get it working with "get" you can switch over to "post" to hide the values.
Also make sure your div tags are within the form tags.
I'll try to look at it within my jsp environment to make sure it works for me. I'll let you know.
[ August 02, 2003: Message edited by: John Hembree ]
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you going to have the file upload in there as well on this page. If you do that will add a whole new way of getting to the data?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Hembree:
Are you going to have the file upload in there as well on this page. If you do that will add a whole new way of getting to the data?

Don't know yet. Put it there just in case and was gonna deal with it later. For now we can just assume it won't be there.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW - here is how I am getting the Authors
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And in posting that I just found out why it is not working. I left the s off of author in the loop that gets the rest of the authors. Geeeeeez!!
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That gets me at times as well not to mention the case sensitivity of java...
Sounds like your on your way then, until next time.
[ August 02, 2003: Message edited by: John Hembree ]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, fixed that and all is well with the world. I am getting all my params like I am supposed to be doing.
Thanks for all your help John.
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
... and all is well ...

I was looking at the form again and noticed something on the remove which may have an adverse effect on your user. Add some text in Author1 and then click add, add text to Author2 and repeat thru Author3,4,5 or whatever. If you remove one you lose all of your typed text except for Author1, where'd it go? You should be able to retain any values in your form for the user.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Hembree:

I was looking at the form again and noticed something on the remove which may have an adverse effect on your user. Add some text in Author1 and then click add, add text to Author2 and repeat thru Author3,4,5 or whatever. If you remove one you lose all of your typed text except for Author1, where'd it go? You should be able to retain any values in your form for the user.

Yes, I noticed that too. I have fixed that. I just had to keep track of another array on each remove. Simpler fix than I originally thought when I saw that happen.
Thanks.
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
Sorry, but I know very little about Javascript. If you could provide a good link to this information, that would be good enough.

http://hotwired.lycos.com/webmonkey/programming/javascript/
[ August 02, 2003: Message edited by: Jason Menard ]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jason Menard:

http://hotwired.lycos.com/webmonkey/programming/javascript/
[ August 02, 2003: Message edited by: Jason Menard ]

Thanks Jason.
 
Lih Chang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm new in this field. Is it possible to have both Type (the drop down box) and Name replicated when you click add Author as seen in the example Link? Thanks in advance.
[ August 05, 2003: Message edited by: Lih Wang ]
[ August 05, 2003: Message edited by: Lih Wang ]
 
Lih Chang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all. I got it figured out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!