Re: Pass integer value from a checkbox on JSP to the server
albert kao wrote:
How to pass integer value from a checkbox on JSP to the server?
None of the following 3 checkboxes will pass an integer to the server.
The server receive "k" or "$k" but not 1, 2, 3, ....
<td><input type="checkbox" name="choice" value="k">Data</td>
You are telling the checkbox that its value is the string "k".
<td><input type="checkbox" name="choice" value="$k">Data</td>
You are telling the checkbox that its value is the string "$k".
<td><input type="checkbox" name="choice" value=k>Data</td>
You are telling the checkbox that its value is the string "k".
You should get in the habit of enclosing all HTML attribute values in quotes.
The JSP page is:
<c:set var="k" value="0"/>
<c:forEach items="${model.data}" var="data">
<tr>
<c:forEach items="${data}" var="datavalue">
<td style="font-family:monospace">${datavalue}</td>
</c:forEach>
<td><input type="checkbox" name="choice" value="k">Data</td>
<c:set var="k" value="$(k+1)"/>
</tr>
</c:forEach>
You want to use an Expression Language (EL) expression.
<http://www.ibm.com/developerworks/java/library/j-jstl0211.html>
Section on "Variable tags" may be most useful here.
<http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPIntro7.html>
<http://java.sun.com/products/jsp/syntax/2.0/syntaxref207.html>
<http://www.oracle.com/technology/sample_code/tutorials/jsp20/simpleel.html>
*Expression Language Syntax*
The JSP expression language allows a page author to access a bean
using a simple syntax such as:
${expr}
In the above syntax, expr stands for a valid expression. It must be
noted that this expression can be mixed with static text, and may also
be combined with other expressions to form larger expressions.
--
Lew