• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

Validate Dynamic Checkbox (multiple values)

Hello all.  I have an app that allows a user to apply multiple Courses and Required Reading to a new Qual.  So, the user will type in the name of the new qual, and from a series of checkboxes (quantity varies), they will assign various reading and courses to this new qual.  I need to be able to run though each checkbox and see if it has been checked, and if so, I need to enter the values in the database.  The problem that I am having is that each checkbox will have 2 values, e.g. CourseNO and CourseDesc.  These are two separate fields in the database for other reasons.  So, how would I cycle through them and get these two values?

I am creating the checkboxes with the following code:
  <% While Not rsGetCourse.EOF %>
  <tr>
    <td align="right">&nbsp;</td>
    <td><label> <input type="checkbox" name="cbxReqCourse" id="cbxReqCourse" /> </label><%=rsGetCourse("dbCourseNo")%> - <%=rsGetCourse("dbCourse")%></td>
    <td>&nbsp;</td>
  </tr>
  <% 
x = x+1
rsGetCourse.MoveNext 
Wend 
%>

Open in new window


Would I just put the value of the checkbox to both variables separated by some special character and then just use some sort of Trim function to separate the variables and add them to the database?
Thanks,

Mike
0
tmss_it_dept
Asked:
tmss_it_dept
  • 5
  • 3
2 Solutions
 
tmss_it_deptAuthor Commented:
Actually, what I might do is to use the Primary Key as the Value of the checkbox, and then as I go though to see what checkboxes have been checked, if it has been checked, I will run another query to get the CourseNo and CourseDesc using the primary key and then write that to the database.  Sound feasible?

Mike
0
 
leakim971PluritechnicianCommented:
>Sound feasible?

Yes, why not?
You may use hidden fields too.

0
 
SRigneyCommented:
the best solution is making the value of the checkbox the id of the value in the table, and you can look it up.  when you submit checkboxes you'll get an array of the checkboxes and can get access to the value.  

If for some reason you cannot use an id in there you could still set the value to the CourseNo|Course Name with a pipe delimiter or whatever delimiter would not be part of the course name.   Then you can split this value in code on the delimiter and use the 2 values that way.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
tmss_it_deptAuthor Commented:
Yeah, I was actually working on the first method (ID as checkbox value) now, so will let you know the results/problems.  :)

Mike
0
 
tmss_it_deptAuthor Commented:
Ok, so I did get stuck.  Being that I am using javascript to check certain fields are filled in before submitting, I am assuming that I need to create the Array() using javascript as well???  If so, how would I use this array to populate the database? OR, if I am using vbscript for the array, then how would I check the values of the the dynamic checkboxes?

Mike
0
 
SRigneyCommented:
You can do your validation in javascript before submitting the form.  On the server-side everything with the same name is submitted as an array.

If you are looking to verify something in the checkboxes, like one of them must be selected.  then you can do something like

if( $("input[name=cbxReqCourse]:checked").length() > 0 ) {
// at least one checkbox was checked.
}
0
 
tmss_it_deptAuthor Commented:
Ok, you say that everything on the server-side (same name) is submitted as an array, is that array called cbxReqCourse?  As far as what I am trying to do is to find out which checkboxes have been checked, and then enter records in the database based on which ones are checked.

Thanks,

Mike
0
 
SRigneyCommented:
If you are using classic ASP then this will probably help you out.  http://www.codeproject.com/KB/database/checkboxesfordatabase.aspx
0
 
tmss_it_deptAuthor Commented:
That is a very good article.  I will definately put it in my archives.  I found a snippet similar to the one you mentioned earlier about using a hidden field and using a special character to separate values and then extract them later.  Instead of using a hidden field though, it was just using a variable basically the same.  I am then sending that variable in string to another page for processing.

Thanks for the ideas,

Mike
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now