Check the CheckBox in your code based on the selection on RadioButton : Form CheckBox « JSP « Java Tutorial






<HTML>
    <HEAD>
        <TITLE>Design Your Car!</TITLE>
    </HEAD>
 
    <BODY>
    <% 
        String aCar = "", bCar = "", cCar = "";
        String check1 = "", check2 = "", check3 = "";
        if(request.getParameter("radios") != null) {
           if(request.getParameter("radios").equals("aCar")){
               aCar = "CHECKED";
               check2 = "CHECKED";
           }
           if(request.getParameter("radios").equals("bCar")){
               bCar = "CHECKED";
               check1 = "CHECKED";
               check3 = "CHECKED";
           }
           if(request.getParameter("radios").equals("cCar")){
               cCar = "CHECKED";
               check1 = "CHECKED";
               check2 = "CHECKED";
               check3 = "CHECKED";
           }
       }
    %>

        <H1>Design Your Car!</H1>
        <FORM METHOD="POST">
            <TABLE BORDER="1" ALIGN="LEFT">
                <TR>
                    <TH>
                        Car
                    </TH>
                </TR>
                <TR>
                    <TD>
                        <INPUT TYPE="radio" NAME="radios" VALUE="aCar"
                            <%= aCar %>
                        >
                       Cheese Car
                       <BR>
                       <INPUT TYPE="RADIO" NAME="radios" VALUE="bCar"
                           <%= bCar %>
                       >
                       Turkey Car
                       <BR>
                       <BR>
                       <INPUT TYPE="RADIO" NAME="radios" VALUE="cCar"
                            <%= cCar %>
                       >
                       Ham Car
                       <BR>
                   </TD>
               </TR>
            </TABLE>
    
            <TABLE BORDER="1" ALIGN="LEFT">
                <TR>
                    <TH>
                        Colors
                    </TH>
                </TR>
                <TR>
                    <TD>
                        <INPUT TYPE="CHECKBOX" NAME="check1" VALUE="check1"
                            <%= check1 %>
                        >
                        Red
                        <BR>
                        <BR>
                        <INPUT TYPE="CHECKBOX" NAME="check2" VALUE="check2"
                            <%= check2 %>
                        >
                        Yellow
                        <BR>
                        <BR>
                        <INPUT TYPE="CHECKBOX" NAME="check3" VALUE="check3"
                            <%= check3%>
                        >
                        Black
                    </TD>
                </TR>
            </TABLE>
            <BR CLEAR="ALL">
            <BR>
            <INPUT TYPE="SUBMIT" VALUE="Submit">
        </FORM>
    </BODY>
</HTML>








23.17.Form CheckBox
23.17.1.Check the CheckBox in your code based on the selection on RadioButton
23.17.2.Read Form Checkboxes