Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

checkboxes

Posted on 2004-04-26
22
Medium Priority
?
791 Views
Last Modified: 2010-04-01
There are several records listed and each has its own checkbox. When the checkbox is checked and the form submitted i need to update a field in a mysql table of those records that were checked.  how would i identify each checkboxe uniquely if the number of records on the page is dynamic.  Can i use the id of the record in the html below as a unique identifer?

<td width="89">
    <form name="form1" method="post" action="">
    <input type="checkbox" name="checkbox" value="checkbox">
</form></td>
0
Comment
Question by:Raftor
  • 10
  • 9
  • 2
  • +1
22 Comments
 
LVL 7

Expert Comment

by:searlas
ID: 10916865
Yes, something like:
<input type="checkbox" name="checkbox" value="<%= recordId %>">
0
 

Author Comment

by:Raftor
ID: 10917505
how would i check if that checkbox was checked when i hit submit?
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10917842
when you submit checkboxs it gets transmitted as an array. So you will have to loop though that array and test which values have been checked.

http://www.javaworld.com/javaworld/jw-03-2000/jw-0331-ssj-forms.html

might be helpful there are 4 pages of various approaches to your problem.
Take care,
Ghost
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 32

Expert Comment

by:ldbkutty
ID: 10917879

>    <input type="checkbox" name="checkbox" value="<%= recordId %>">

When you hit the submit button only the checked boxes will be passed to the next page.

As said, u can use the id of the records in the value and so the checkbox that are checked only pass to the next page.
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10917891
:) thanks for confirming :)
Cheers,
Ghost
0
 
LVL 7

Expert Comment

by:searlas
ID: 10918052
The simple answer to reading which checkboxes are checked would be, in your JSP/Servlet:

String checkedRecords[] = requeset.getParameterValues("checkbox");
for (int i = 0; checkedRecords != null && i < checkedRecords.length; i++) {
    String recordId = checkedRecords[i];
    // do something with a record
}

0
 

Author Comment

by:Raftor
ID: 10919925
This is the code im using to get the checked values. Do i have to set it to a session variable?
<td width="89"><form name="form1" method="post" action="">
            <input type="checkbox" name="<%=strId %>" value="<%= strId %>">
</form></td>

This is the code im using to try to print it out on the jsp page.
<%
      String checkedRecords[] = request.getParameterValues("checkbox");
      for (int i = 0; checkedRecords != null && i < checkedRecords.length; i++)
      {
          String recordId = checkedRecords[i];
          out.print(recordId);
          %>
                recordId is: <% out.print(recordId); %>         
          <%
      }
%>

but nothing is being printed out, what am i doing wrong?
0
 

Author Comment

by:Raftor
ID: 10920037
Sorry. The first piece of code is actually this:

<td width="89"><form name="form1" method="post" action="">
          <input type="checkbox" name="checkbox" value="<%= strId %>">
</form></td>
0
 

Author Comment

by:Raftor
ID: 10920164
Its important i get this done sone so ill put the points up to 125.
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10920706
<td width="89"><form name="form1" method="post" action="myJsp.jsp">
          <input type="checkbox" name="checkbox" value="<%= strId %>">
</form></td>

will only give you one checkbox..

you have to put this code on another page, the page you are posting your information too..

<%
     String checkedRecords[] = request.getParameterValues("checkbox");
     for (int i = 0; checkedRecords != null && i < checkedRecords.length; i++)
     {
         String recordId = checkedRecords[i];
         out.print(recordId);
         %>
              recordId is: <%= recordId %>        
         <%
     }
%>

that should work
Cheers,
Ghost
0
 

Author Comment

by:Raftor
ID: 10920798
i have that code on another page and when i go to print out the values it tells me they are null.  It must be something to do with the way i am sending them do you think?
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10920813
are you sure the variable "strId" is not null?
Ghost
0
 

Author Comment

by:Raftor
ID: 10920851
The think about this piece of code is that its dynamic. There is no set amout of checkboxes, could this be the problem?
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10920856
no that shouldn't matter..
I'm testing yout code now.
I'll have it solved for you soon... I hope :)
Ghost
0
 

Author Comment

by:Raftor
ID: 10920858
No its definately not null because i print it out on that very page.
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10920887
alright well it works for me..

test.jsp

<form name="form1" method="post" action="test3.jsp">
   <input type="checkbox" name="boxes" value="1" />1<br />
   <input type="checkbox" name="boxes" value="2" />2<br />
   <input type="checkbox" name="boxes" value="3" />3<br />
   <input type="checkbox" name="boxes" value="4" />4<br />
   <input type="submit" value="GO" />
</form>


test3.jsp

<%
     String checkedRecords[] = request.getParameterValues("boxes");
     for (int i = 0; checkedRecords != null && i < checkedRecords.length; i++)
     {
         String recordId = checkedRecords[i];
         out.print(recordId);
         %>
              recordId is: <%= recordId %> <br />
         <%
     }
%>

and it works fine..
try it out.
Ghost
0
 

Author Comment

by:Raftor
ID: 10920953
ya that worked fine alright.  The thing is im only using the line "<input type="checkbox" name="boxes" value="1" />1<br />" once and looping around again to print it out for another record if there is one in the database. Its like this:

<%      
      String inServer = "localhost";
      String inDBName = "colin";
      String DATABASE_URL = "jdbc:mysql://" + inServer + ":3306/" + inDBName;

      Class.forName("com.mysql.jdbc.Driver").newInstance();
      Connection con = DriverManager.getConnection(DATABASE_URL, "colinrafter", "keyhole");
      Statement stmt = con.createStatement();            
      
      ResultSet rs = stmt.executeQuery("SELECT prod_id,maker,name,description,est_cost,quantity,requested_by,date,approved FROM requests");             
      
      int cnt = 0;
      
      String strId = "";
      String strMaker = "";
      String strName = "";
      String strDescription = "";
      String strEst_cost = "";
      String strQuantity = "";
      String strRequested_by = "";
      String strDate = "";
      String strApproved = "";
      
      String[] arr = "";
      
      while(rs.next())
      {
            cnt += 1;
            
            strId = rs.getString(1);
            strMaker = rs.getString(2);
            strName = rs.getString(3);
            strDescription = rs.getString(4);
            strEst_cost = rs.getString(5);
            strQuantity = rs.getString(6);
            strRequested_by = rs.getString(7);
            strDate = rs.getString(8);
            strApproved = rs.getString(9);



      %>
            <table width="700" border="1">
            <!-- //bgcolor = 6495ED> -->
            <tr>
            <th width="54"><% out.print(strId); %></th>
            <td width="95"><% out.print(strMaker); %></td>
            <td width="100"><% out.print(strName); %></td>
            <td width="129"><% out.print(strEst_cost); %></td>
            <td width="167"><% out.print(strQuantity); %></td>
            <td width="263"><% out.print(strRequested_by); %></td>
            <td width="131"><% out.print(strDate); %></td>
            <td width="131"><% out.print(strApproved); %></td>
            
            <td width="89">
                  <form name="form1" method="post" action="approvals.jsp">
                      <input type="checkbox" name="checkbox" value="<%= strId %>">
            </form>
            </td>

            </tr>

            </table>

      <%

      }
%>
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10921125
that shouldn't matter try this..

test.jsp

<form name="form1" method="post" action="test3.jsp">
<%
int count = 5;
while (count > 0) {
%>
   <input type="checkbox" name="boxes" value="<%=count%>" /><%=count%>
<%
count--;
}
%>
<input type="submit" value="GO" />
</form>


and use the same code for test3.jsp

Ghost
0
 

Author Comment

by:Raftor
ID: 10921206
ya that worked too!  I cant understand it, does my code structure look ok?
0
 
LVL 13

Accepted Solution

by:
copyPasteGhost earned 500 total points
ID: 10921269
change your code to this..
<%    
     String inServer = "localhost";
     String inDBName = "colin";
     String DATABASE_URL = "jdbc:mysql://" + inServer + ":3306/" + inDBName;

     Class.forName("com.mysql.jdbc.Driver").newInstance();
     Connection con = DriverManager.getConnection(DATABASE_URL, "colinrafter", "keyhole");
     Statement stmt = con.createStatement();          
     
     ResultSet rs = stmt.executeQuery("SELECT prod_id,maker,name,description,est_cost,quantity,requested_by,date,approved FROM requests");          
     
     int cnt = 0;
     
     String strId = "";
     String strMaker = "";
     String strName = "";
     String strDescription = "";
     String strEst_cost = "";
     String strQuantity = "";
     String strRequested_by = "";
     String strDate = "";
     String strApproved = "";
     
     String[] arr = "";
 %>
<form name="form1" method="post" action="approvals.jsp">
<%
     while(rs.next())
     {
          cnt += 1;
         
          strId = rs.getString(1);
          strMaker = rs.getString(2);
          strName = rs.getString(3);
          strDescription = rs.getString(4);
          strEst_cost = rs.getString(5);
          strQuantity = rs.getString(6);
          strRequested_by = rs.getString(7);
          strDate = rs.getString(8);
          strApproved = rs.getString(9);



     %>
          <table width="700" border="1">
          <!-- //bgcolor = 6495ED> -->
          <tr>
          <th width="54"><% out.print(strId); %></th>
          <td width="95"><% out.print(strMaker); %></td>
          <td width="100"><% out.print(strName); %></td>
          <td width="129"><% out.print(strEst_cost); %></td>
          <td width="167"><% out.print(strQuantity); %></td>
          <td width="263"><% out.print(strRequested_by); %></td>
          <td width="131"><% out.print(strDate); %></td>
          <td width="131"><% out.print(strApproved); %></td>
         
          <td width="89">
                 <input type="checkbox" name="checkbox" value="<%= strId %>">

          </td>

          </tr>

          </table>

     <%

     }
%>
          <input type="submit" name="submit" value="Click To Send" />

</form>
0
 

Author Comment

by:Raftor
ID: 10921299
Brilliant!!!

Thats worked, thanks :)
Raftor.
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10921317
sure no problem
glad I can help
Ghost
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month12 days, 8 hours left to enroll

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question