Link to home
Start Free TrialLog in
Avatar of tmss_it_dept
tmss_it_deptFlag for United States of America

asked on

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
Avatar of tmss_it_dept
tmss_it_dept
Flag of United States of America image

ASKER

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
Avatar of leakim971
>Sound feasible?

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

SOLUTION
Avatar of SRigney
SRigney
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah, I was actually working on the first method (ID as checkbox value) now, so will let you know the results/problems.  :)

Mike
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
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.
}
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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