Information sent in an HTML form

I've a HTML file that shows to the user the contents of a database (it is shown as a table). The user can choose one of the rows.When this is done the selection made by the user is sent to a servlet that will work with that information.

Imagine that this servlet is going to look for files related to the information chosen by the user. What I'd like to do is to provide the user with the option of also choosing the number of files that are going to be looked for by the servlet. That way the user should be able to choose one of the rows shown in the table and should also be able of typing the numers of files that are to be looked for.

So far I'm able to send to the servlet what the user chooses in the table, but I'd like to know if it is possible to attach to this information the number of files requested.

This is my code:

<center><form action="administ" method=post >                           
<center><table>
<table border=\"1\"><tr><th></th><th>Titleo</th><th>Author</th><th>Album</th></tr>
<c:forEach items="${items}" var="item">
<tr>                                
<td><input type="radio" name="Song" value="${item.file}@${item.title}#${item.author}$${item.album}">
<td>${item.title}</td>
<td>${item.author}</td>
<td>${item.album}</td>
</tr>
</c:forEach>
</table></center>
<tr><td colspan=2><input type=submit value = play name = option></td></tr>
<tr><td colspan=2><input type=submit value = Delete name = option></td></tr>

At this point I want to add a new option that requires not only a new button, but also requires the user to introduce a number.

Answers


That depends. If you want single selection of rows using radiobuttons, then you could just put a single input field at bottom of table, next the submit button or so. E.g.:

<input type="text" name="numberOfFiles">

which you can obtain in the servlet as follows:

String numberOfFiles = request.getParameter("numberOfFiles");

But if you want multiple selection of rows using checkboxes or if you want this field to appear in each row at any way, then you need to give the radio/checkbox field a value of the row index. If you're using JSTL <c:forEach> to iterate over the rows (which I'd expect that you indeed do), then you can make use of the varStatus attribute to declare a LoopTagStatus. Inside the loop you can obtain the row index by LoopTagStatus#getIndex(). E.g.:

<table>
    <c:forEach items="${items}" var="item" varStatus="loop">
        <tr>
            <td><input type="checkbox" name="selected" value="${loop.index}"></td>
            <td><input type="text" name="number"></td>
        </tr>
    </c:forEach>
</table>
<input type="submit">

(to have single selection, just replace type="checkbox" by type="radio")

In the servlet, you can obtain all input fields with the same name in the order as they appear in the table as follows:

String[] numbersOfFiles = request.getParameterValues("numberOfFiles");

With checkbox-selection you can obtain the all selected row indexes and thus also the associated input field as follows:

String[] selectedIndexes = request.getParameterValues("selected");
for (String selectedIndex : selectedIndexes) {
    int index = Integer.parseInt(selectedIndex);
    String numberOfFiles = numbersOfFiles[index];
    // ...
}

Or if it is a radiobutton-selected row which is single selection at any way:

String selectedIndex = request.getParameter("selected");
int index = Integer.parseInt(selectedIndex);
String numberOfFiles = numbersOfFiles[index];

Need Your Help

How can I do multiple async functions (not nested) for performance, but wait for them to finish before proceeding

javascript asynchronous

I've been using Golang for quite some time, but I love writing Javascript so I've switched back, but in Golang you could use sync.WaitGroup to perform multiple goroutines and wait for them to finis...

Displaying a Float to a Textbox type "number"

c# asp.net textbox floating-point

There is a lot of questions regarding the conversion of a Textbox string to a float value, or allowing a Textbox of type="number" to allow decimal points, however, I can't seem to find anything rel...