Is this what they call the DYNAMIC FORMs
?
HTML
<div id="myForm">
<form method="post">
<input type="text" name="question1" placeholder="Question 1"/>
<input type="text" name="item1" placeholder="Item 1"/>
<a href="#" onclick="addItem();">Add more items</a>
<a href="#" onclick="addQuestion();">Add new question</a>
</form>
</div>
<script>
function addQuestion() {
// codes to add more input type=text like:
$('#myForm').append('<input type="text" name="item2"/>');
}
function addItem() {
// codes to add more input type=text like:
$('#myForm').append('<input type="text" name="item2"/>');
}
</script>
As a student, it is really complicated for me to think about these stuffs.
My problem is that, I do not have any idea on how to pass multiple items or data on a form. And even what to put on a name
attribute of an <input>
. Also, if the user add more items! How could I pass these unexpected number of data.
Example also when the user wants to add more items or options.
Or should I implement these logic:
<form>...</form>
<a href="#" onclick="addForm();">Add a question</a>
<script>//codes to add new form</script>
And what I'm gonna do is just know how to pass multiple form. But if I do that, will the Struts2
(request.getParameter()
in mvc) to identify the data passed?
Please help,, this is just so complicated from a student like me.
EDITED: The data to be pass is like a multi-dimensional array. For example:
[question1, [item1, item2, item3]],
[question2, [item1, item2, item3, item4]]
[question3, [item1, item2]]
In other words, in the form, the user can add more question, and in each question the user can add more items.
The main function is that the user can create multiple-choice
quiz. So in each questions, there would be any number of item. Where item = options or choices
.
You can map request variables to a list of beans in struts2. Assuming you have the Parameters interceptor defined or are using the defaultStack.
1) Define a bean that contains
2) Expose this bean as a list in your action class
3) In your jsp send the request parameters like this.
You will have to consider what do when clicking the Add question button, in my example I am assuming that you want to reset the item count for the new question.
Here is a similar question about tabular form inputs in struts2. POST an array of custom objects to a Struts 2 action Hope this gives you an idea.