Solved

Validate Dynamic Checkbox (multiple values)

Posted on 2011-03-08
9
346 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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
 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

803 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