Solved

Validate Dynamic Checkbox (multiple values)

Posted on 2011-03-08
9
342 Views
Last Modified: 2012-05-11
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
Comment
Question by:tmss_it_dept
  • 5
  • 3
9 Comments
 

Author Comment

by:tmss_it_dept
ID: 35074790
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
 
LVL 82

Expert Comment

by:leakim971
ID: 35082437
>Sound feasible?

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

0
 
LVL 15

Assisted Solution

by:SRigney
SRigney earned 125 total points
ID: 35083881
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
 

Author Comment

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

Mike
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:tmss_it_dept
ID: 35085919
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
 
LVL 15

Expert Comment

by:SRigney
ID: 35086645
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
 

Author Comment

by:tmss_it_dept
ID: 35086839
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
 
LVL 15

Accepted Solution

by:
SRigney earned 125 total points
ID: 35088744
If you are using classic ASP then this will probably help you out.  http://www.codeproject.com/KB/database/checkboxesfordatabase.aspx
0
 

Author Comment

by:tmss_it_dept
ID: 35095829
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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 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

15 Experts available now in Live!

Get 1:1 Help Now