Solved

checkboxes

Posted on 2004-04-26
22
789 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Successful collaboration among team members is essential for the growth of your business. When employees work together on projects, share ideas and communicate effectively they get better results.
This article describes how to import Lotus Notes Contacts into Outlook 2016, 2013, 2010 and 2007 etc. with a few manual steps. You can easily export and migrate Lotus Notes contacts into Microsoft Outlook without having to use any third party tools.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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