Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to get checkbox values?

    Hello

    I have a form in which radio buttons, text field, drop down menu & check boxes are present. After entering data in form, I get values of text field, radio buttons & drop down menu thru Jsp but problem is in getting check box values since users can click multiple check boxes. Text field value can be got thru its name parameter but there are many check boxes.

    Here is the code :

    <body bgcolor="#ffffff">
    <form method=GET action="getdata.jsp" name="try">
    <table border="1">
    <tr>
    <td>Field1 : </td>
    <td><input type="text" name="txt" value="hi"></td>
    </tr>

    <tr>
    <td>Field2 :</td>
    <td><input type="checkbox" name="c1" value="c1check">C1&nbsp;&nbsp;&nbsp;

    <input type="checkbox" name="c2" value="c2check" checked>C2&nbsp;&nbsp;&nbsp;

    <input type="checkbox" name="c3" value="c3check">C3&nbsp;&nbsp;&nbsp;

    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>


    JSP CODE :

    //getting value of textfield
    <% String Field1=request.getParameter("txt");%>
    Field 1 : <%=Field1%>
    <br>

    //Only one checkbox value is coming.
    <% String Field2=request.getParameter("c1");%>
    Field 2 : <%=Field2%>

    How to know which is check & get its value?

    Looking forward for an early reply.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,946
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Just like radio buttons and select boxes, only selected/checked items get submitted to the server. So if you request the parameter and it returns null, it means it is not checked. If not null, then the returned value is the value of the checked item.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •