So I have been reading for the last few days on JSP and HTML. I read that it is not possible to have nested forms in HTML.
Does anyone have any indication of how to do this? Is there some JSP code that can aid in this? There is no such thing in my JSP/Java book.
See I have a form, to which there are elements that the user can dynamically add or delete - including new forms. So you can think of it as a hierarchy - I have a parent form, with X (user defined) children, each child can have Y (user defined) children..etc So when a form is submitted, it creates a new form inside this form - then when the outer most form is submitted, all the details of the forms and subforms are then compounded into a report (I havent decided what I am going to do with this information). All this information is location specific, so the nesting is mandatory.
I have produced a hack, with global variables and some javascript -开发者_开发知识库 but it is vulnerable, and most likely will break when the concept of 'users' are added, with sessions etc. So I do not want to use this at all, given that it will be painful to debug.
Thanks, U.
Your first statement is right; you can't nest forms in HTML.
If you write something that looks like HTML with nested forms, you'll probably find that the behaviour differs between browsers. Some may cut off the form fields after the first </form>
block, some may submit all the field elements, some may work as you expect.
If you data is hierarchical in nature as your post suggests, then they way I would probably handle it is to have a single <form>
element with many <submit>
buttons. You can then choose to only examine the form fields related to the button that the user clicked. e.g. if they click "Child 1", then your JSP just looks at the fields related to "Child 1".
You'll need to make sure that your field names are uniquely named, though - e.g. by some sort of hierarchical naming scheme - e.g. <input type="text" name="Parent_Child1_name">
After reading your description I immediately thought of jQuery and JavaScript. You could dynamically modify the DOM to add and remove HTML elements as needed rather than nesting <form>
elements. I'd recommend giving that a try.
精彩评论