简体   繁体   中英

how to pass checkbox value(checked/unchecked) to controller as href parameter in jsp

I am a beginner in jsp.I want to get data from database and show checked on a checkbox depending on db value ie 0 or 1.This is working for me.But I also want o that when I check or uncheck the checkbox it will reset value and pass it to controller as a href parameter. here is my jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
    <title>Investor Account</title>
</head>

    <body>

  <script type="text/JavaScript">
            function updateCheck(){

                if(document.getElementById('chk').checked){
                     document.getElementById('chk').value = 1;
                     location.href=""
                     alert(document.getElementById('chk').value);



                 }
                else{
                      document.getElementById('chk').value = 0;
                      alert(document.getElementById('chk').value);


                }

            }

</script>
        <div align="center">
            <h1>Investor Account List</h1>

            <table border="1">
                <th>ID</th>
                <th>Investor Code</th>
                <th>Investor Name</th>
                <th>SMS Subscriber</th> 
                <th>Action</th>         
                <c:forEach var="investorAcc" items="${listInvestorAcc}" varStatus="st">
                <tr>
                    <td>${st.index + 1}</td>
                    <td>${investorAcc.investor_code}</td>
                    <td>${investorAcc.investor_name}</td>
                    <td> <input type="checkbox" id="chk" name="chkSms" value=  <c:if test="${investorAcc.sms_sub==1}">1</c:if> onclick="updateCheck();"
                    <c:if test="${investorAcc.sms_sub==1}">checked="checked"</c:if>/>

                  <td>

                      <a href="updateInvestorAcc?id=${investorAcc.id}&chkSms=<c:if test="${chkSms==1}">1</c:if>"> Update </a>
                   </td>

                </tr>

                </c:forEach>             
            </table>
        </div>
    </body>
</html>

To update checkbox value, the easiest way is to use form in loop. Try

<c:forEach var="investorAcc" items="${listInvestorAcc}" varStatus="st">
   <form action="updateInvestorAcc">
      <input type="hidden" name="id" value="${investorAcc.id}"/>
      <tr>
          <td>${st.index + 1}</td>
          <td>${investorAcc.investor_code}</td>
          <td>${investorAcc.investor_name}</td>
          <td>
              <c:choose>
                 <c:when test="${investorAcc.sms_sub==1}">
                   <input type="checkbox" id="chk" name="chkSms" 
                        value="${investorAcc.sms_sub}" checked="checked"/>
                 </c:when>
                 <c:otherwise>
                     <input type="checkbox" id="chk" name="chkSms" 
                           value="${investorAcc.sms_sub}"/>
                 </c:otherwise>
              </c:choose><td> <!-- end of checkbox -->
          <td><input type="submit" value="Update"></td>
      </tr>
   </form> 
</c:forEach>

Edit:

You need a Servlet to get updated value

@WebServlet("/updateInvestorAcc")
public class UpdateInvestorAcc extends HttpServlet {

 public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {

     String idStr=request.getParameter("id");
     int id= Integer.parseInt(idStr);// If you need to parse to int
     String[] chkSms=request.getParameterValues("chkSms");
     boolean isChkSms =false;
     if(chkSms !=null && chkSms.length > 0){//If checkbox is checked than assign it with true or 1       
         isChkSms=true;  
     }

    // Now update the DB with id and checkbox value.
 }

you can simplify the middle bit in the other posters suggestion with a basic IF test. Because it's only going to be 1 or 0 (choose is overkill IMO). Technically because it's 1/0 you don't even need to ==1 comparitor, just reference it raw in the test="" block as test="${investorAcc.sms_sub}". The JSTL is smart enough to handle all angles (including NULL). 1=true, 0=false, NULL=false.

<c:forEach var="investorAcc" items="${listInvestorAcc}" varStatus="st">
<form action="updateInvestorAcc">
    <input type="hidden" name="id" value="${investorAcc.id}"/>
    <tr>
        <td>${st.index + 1}</td>
        <td>${investorAcc.investor_code}</td>
        <td>${investorAcc.investor_name}</td>
        <td>
            <input  type="checkbox" id="chk" name="chkSms" 
                    value="${investorAcc.sms_sub}" <c:if test="${investorAcc.sms_sub==1}">checked="checked"</c:if>/>
        <td> <!-- end of checkbox -->
        <td><input type="submit" value="Update"></td>
    </tr>
</form> 
</c:forEach>

For your second bit about the 'unchecked' box passing invalid values. That's a super annoying thing with HTML. Unchecked boxes get 'dropped to null' when a form is submitted. A lot of people are supplementing with a 2nd hidden input to get this done without NULL errors (note, it MUST be second). Different "ID", but same "NAME" so the Servlet will see it and capture the 'unchecked' value from it instead of the NULLed original checkbox:

ex:

<td>
    <input  type="checkbox" id="chk" name="chkSms" 
            value="${investorAcc.sms_sub}" <c:if test="${investorAcc.sms_sub==1}">checked="checked"</c:if>/>
    <input  type="hidden" id="chk_0" name="chkSms"
            value="0"/>
<td> <!-- end of checkbox -->

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM