dimanche 23 avril 2017

iterating in "for" loop and "if" statments with JSP

I'm pretty new in coding and WEB development with JSP. I'm trying to create dynamic form made only from checkboxes when the general idea is when specific user check a specific checkbox, the information of this specific checkbox together with the user id and some index related to user information is being sent to a database and when this user or an other users loading this page the information is being retrieving from data base and it checking the pre checked checkboxes and disable them and assigning each and every checked checkbox its relevant color depending on the index number I've set to the relevant users.

The problem is when i run my code it retrieve data from DB and check and disable the boxes, however the color is set only once depending the first "true" condition in the if statement, so for example if the first line it query (actually last line in mysql) is with index "2" it would set all checked boxes with the relevant color set in the "if" statement even the code keep iterating and entering other "if" statements with other color settings.

Sorry about the long story, hope i was clear enough for someone to help me. I'm running out of options.

Here are the relevant cods legs:

form:

<form method="post" action="confirm.jsp" id="form" name="form">
<input type="checkbox" id="ckb" name="chair"  value=""/>
<input type="checkbox" id="ckb" name="chair" value="" />
<input type="checkbox" id="ckb" name="chair"  value=""/>
<input type="checkbox" id="ckb" name="chair" value="" />
<input type="checkbox" id="ckb" name="chair"  value=""/>
<input type="checkbox" id="ckb" name="chair" value="" />
<input type="checkbox" id="ckb" name="chair"  value=""/>
<input type="checkbox" id="ckb" name="chair" value=""/>
<input type="checkbox" id="ckb" name="chair"  value=""/>
<input type="checkbox" id="ckb" name="chair" value=""/>
</form>

<input type="button"  id="sendNewSms" name="sendNewSms" value"Submit"    onclick="submitForms()"  >

<script>
submitForms = function(){
document.getElementById("form").submit();}
</script>

JS function with JAVA:

<script type="text/javascript">   
function keepBoxChecked(){

        <%for (int i=0; i< 10; i++){

           if(userIndex[i].equals("1")){ 
                boxColor = "red"; %>
                document.form.ckb[<%=boxChecked[i]%>].checked = true;
                document.form.ckb[<%=boxChecked[i]%>].disabled = true; <%} 

                else if(userIndex[i].equals("2")){ 
                 boxColor = "blue"; %>
                  document.form.ckb[<%=boxChecked[i]%>].checked = true;
                  document.form.ckb[<%=boxChecked[i]%>].disabled= true;<%} 

                else if(userIndex[i].equals("3")){ 
                 boxColor = "green"; %>
                  document.form.ckb[<%=boxChecked[i]%>].checked = true;
                  document.form.ckb[<%=boxChecked[i]%>].disabled= true;<%} 
            }%>  
}
</script>

CSS:

 input:checked {background-color: <%= boxColor %>;}




Aucun commentaire:

Enregistrer un commentaire