Solved

checkboxes

Posted on 2004-04-26
22
783 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handle form fields in spring mvc controller 1 60
excpetion with multiple catch 11 113
struts hibernate integration example 28 124
I get error: useBean: Duplicate bean name: {0} 1 124
There is no doubt that cloud is gaining importance. Many of you must have read about this technology and its growing importance. More and more organisations are embracing this technology not forgetting start-ups. The process begins by dipping …
: Microsoft Office Collaborate for free and online versions of Microsoft  Word, Excel, Powerpoint, OneNote, Onedrive , Email, Calendar etc. In short we can say that Microsoft office is a suite of servers, applications and services developed by  Micr…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

18 Experts available now in Live!

Get 1:1 Help Now