Solved

checkboxes

Posted on 2004-04-26
22
782 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
Use of TCL script on Cisco devices:  - create file and merge it with running configuration to apply configuration changes
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now