Solved

checkboxes

Posted on 2004-04-26
22
785 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 125 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with Pie chart in Jsp 20 215
Jquery - parsing the results returned from webmethod 1 112
Handle form fields in spring mvc controller 1 65
ejb on wildfly 5 20
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

810 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