473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

how to loop through the values of dynamically generated dropdown lists in classic ASP

24
I have a HTML FORM, inside it,I retrieve multiple records from a table in the database. I populate each record in a row. Each row of the table has a checkbox (its value is the student ID of the record) and a dropdown list that contains 5 different form versions of an exam (A,B,C,D,E). I want the administrator to check the students he wants and choose the form for each student and when click submit button, it updates the form version for the selected students.

Here is a picture that demonstrate my form:



Here is my code which populates the table:
Expand|Select|Wrap|Line Numbers
  1. std_sql = "select ID, SSN, STD_NAME, COLL_DESC, MAJOR_DESC, FORM, CITY, NOTES from kfuban.el_exam_absence"
  2. set std_rs = conntemp.execute(std_sql)
  3.  
  4. Dim mrecord_count
  5. mrecord_count = 0
  6. DO WHILE NOT std_rs.EOF
  7.  
  8. std_id = std_rs(0)
  9. std_ssn = std_rs(1)
  10. std_name = std_rs(2)
  11. std_coll = std_rs(3)
  12. std_major = std_rs(4)
  13. std_form = std_rs(5)
  14. std_city = std_rs(6)
  15. std_notes = std_rs(7)
  16.  
  17. <tr>
  18. <td align="center" width="35"><b><input type="checkbox" name="id_chk" value="<%=std_id%>" style="font-weight: 700"></b></td>
  19. <td align="center" width="35"><b><%response.write mrecord_count+1%></b></td>
  20. <td align="center" width="123"><b><%=std_id%></b></td>
  21. <td align="center" width="124"><b><%=std_ssn%></b></td>
  22. <td align="center" width="240"><b><%=std_name%></b></td>
  23. <td align="center" width="100"><b>
  24.  
  25. <select dir="rtl" name="the_form" style="font-weight: 700">
  26. <option value="" <%if std_form = "" then response.write "selected"%>>Choose Form</option>
  27. <option value="A" <%if std_form = "A" then response.write "selected"%>> &nbsp; - A - &nbsp; </option>
  28. <option value="B" <%if std_form = "B" then response.write "selected"%>> &nbsp; - B - &nbsp; </option>
  29. <option value="C" <%if std_form = "C" then response.write "selected"%>> &nbsp; - C - &nbsp; </option>
  30. <option value="D" <%if std_form = "D" then response.write "selected"%>> &nbsp; - D - &nbsp; </option>
  31. <option value="E" <%if std_form = "E" then response.write "selected"%>> &nbsp; - E - &nbsp; </option>
  32. </select>
  33. </td>
  34. </tr>
  35. <%
  36. std_rs.MoveNext
  37. mrecord_count = mrecord_count + 1
  38. Loop
  39. %>
  40.  
And here is the code when submit the form:
Expand|Select|Wrap|Line Numbers
  1. for i=1 to Request.form("id_chk").Count
  2.  
  3. ids = Request.form("id_chk")(i)
  4. up_form = Request.form("the_form")(i)
  5.  
  6. upSQL="update kfuban.el_exam_absence set FORM = '"&up_form&"' where ID = '"&std_id&"'"
  7. set RS=conntemp.execute(upSQL)
  8.  
  9. next
  10.  
Sometimes, it works perfectly, but sometimes, the value of the submitted form value "up_form" is empty. Anyone know why? Please help me.
Jan 10 '11 #1

✓ answered by jhardman

So basically, correct me if I misunderstand, if you have five students, your form will have a list of 5 checkboxes that list the student ID, and 5 drop-down selects that list options A-E. Then if the admin wants to update this, he checks the checkbox corresponding to the student he wants to change, and changes the drop-down box and hits "submit".

There are two problems with this approach. first, if you have multiple inputs with the same name, then the data is sent as a comma-delimited text stream like this: st_id = "1022, 1023, 1020, 1056" Now if you can guarantee that those will always be in the right order, and will always include all of the data for which you are looking, then your approach of looping through that text as an array would be OK. But as I describe the second problem, maybe you will see what's wrong with that.

The second problem is that if a particular checkbox is left unchecked, then that value is not sent. In other words, if I use that same list I had above, but I only checked the second and fourth checkbox, then the response I get is st_id = "1023, 1056". Do you see the problem? When you loop through these and update your db, you are comparing the first section of this value to the first of the selects, but the first of the checkbox values that is sent is not the first checkbox on your form, it is the first checkbox that was checked. Does that make sense? So your form is submitted with perhaps two student IDs, but all of the selects, and your method has no way of showing which st_id matches which select.

The obvious solution is to number the inputs. Now this works best if you have a fixed number, or a max number of students per form, but even if you assigned an arbitrary number of 200 as the max number of students you should still be able to loop through these very quickly. Here's how I would code it:
Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" name="id_chk<%=mrecord_count%>" value="<%=std_id%>" style="font-weight: 700">
  2. ...
  3. <select dir="rtl" name="the_form<%=mrecord_count%>" style="font-weight: 700">
  4.  
then when I go to loop through the inputs and update the db, I loop like this:
Expand|Select|Wrap|Line Numbers
  1. for i=0 to 200
  2.  
  3.    if request.form("id_chk"&i) <> "" then
  4.       ids = Request.form("id_chk" & i)
  5.       up_form = Request.form("the_form" & i) 
  6.       'include the rest of the update code
  7.       'here, no reason to update if the row
  8.       'wasn't checked
  9.    end if
  10. next
I suppose you could try counting the inputs so you didn't just pick an arbitrary number like 200 like I did, but I've never bothered.

And your approach would work for any input types besides checkboxes. You could probably modify your code to use radio buttons fairly easily.

Let me know if this helps.

Jared

3 6944
jhardman
3,406 Expert 2GB
So basically, correct me if I misunderstand, if you have five students, your form will have a list of 5 checkboxes that list the student ID, and 5 drop-down selects that list options A-E. Then if the admin wants to update this, he checks the checkbox corresponding to the student he wants to change, and changes the drop-down box and hits "submit".

There are two problems with this approach. first, if you have multiple inputs with the same name, then the data is sent as a comma-delimited text stream like this: st_id = "1022, 1023, 1020, 1056" Now if you can guarantee that those will always be in the right order, and will always include all of the data for which you are looking, then your approach of looping through that text as an array would be OK. But as I describe the second problem, maybe you will see what's wrong with that.

The second problem is that if a particular checkbox is left unchecked, then that value is not sent. In other words, if I use that same list I had above, but I only checked the second and fourth checkbox, then the response I get is st_id = "1023, 1056". Do you see the problem? When you loop through these and update your db, you are comparing the first section of this value to the first of the selects, but the first of the checkbox values that is sent is not the first checkbox on your form, it is the first checkbox that was checked. Does that make sense? So your form is submitted with perhaps two student IDs, but all of the selects, and your method has no way of showing which st_id matches which select.

The obvious solution is to number the inputs. Now this works best if you have a fixed number, or a max number of students per form, but even if you assigned an arbitrary number of 200 as the max number of students you should still be able to loop through these very quickly. Here's how I would code it:
Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" name="id_chk<%=mrecord_count%>" value="<%=std_id%>" style="font-weight: 700">
  2. ...
  3. <select dir="rtl" name="the_form<%=mrecord_count%>" style="font-weight: 700">
  4.  
then when I go to loop through the inputs and update the db, I loop like this:
Expand|Select|Wrap|Line Numbers
  1. for i=0 to 200
  2.  
  3.    if request.form("id_chk"&i) <> "" then
  4.       ids = Request.form("id_chk" & i)
  5.       up_form = Request.form("the_form" & i) 
  6.       'include the rest of the update code
  7.       'here, no reason to update if the row
  8.       'wasn't checked
  9.    end if
  10. next
I suppose you could try counting the inputs so you didn't just pick an arbitrary number like 200 like I did, but I've never bothered.

And your approach would work for any input types besides checkboxes. You could probably modify your code to use radio buttons fairly easily.

Let me know if this helps.

Jared
Jan 10 '11 #2
goodamr
24
Hi Jared,

Thanks alot for your response. It works perfectly.

But the only problem is when we have a big list of records (e.g, more than 500 records), it takes some time to be executed.
Jan 11 '11 #3
jhardman
3,406 Expert 2GB
yes, this is a significant issue. The step that takes the longest is updating the db, so as long as not too many records are being updated per form submitted, then it should go really fast. The problem comes when you try to update 50-200 records with one submit. This is exactly the reason that many forms you see online use paged views (Here are the top 20 records, click here to see the next 20). But there is another potential solution:

When I update my db, I usually use the update function of the ADODB.recordset object.When you use this, you don't build an update statement. You load the table into a local "recordset", then make changes to the local recordset, then call update(). The final update call performs all of the updates in one step. I still usually call update() after each row, just so a single error doesn't stop the whole thing, but a single update call after looping through the whole recordset should work much faster than updating each row separately. Here's how the code looks (I've changed the form a little, hopefully it should be obvious).
Expand|Select|Wrap|Line Numbers
  1. 'the form code 
  2. <input type="checkbox" name="id_chk<%=std_id%>" value="<%=std_id%>" style="font-weight: 700">
  3. ...
  4. <select dir="rtl" name="the_form<%=std_id%>" style="font-weight: 700">
Expand|Select|Wrap|Line Numbers
  1. 'the form handler
  2. set objConn = server.createobject("adodb.connection")
  3. objConn.open connectionString
  4.  
  5. set objRS = server.createobject("adodb.recordset")
  6. objRS.open "select * from studentFormTable", objConn, adOpenDynamic, adLockOptimistic
  7. 'those last two arguments are constant 
  8. 'integers, if you haven't linked to the 
  9. 'right include file you can look those up, I 
  10. 'think they are 3 and 4 respectively
  11.  
  12. do until objRS.eof 'loop to the end of the table
  13.    if request.form("id_chk"&objRS("std_id")) <> "" then
  14.       objRS("the_form") = request.form("the_form" & objRS("std_id"))
  15.    end if
  16.    objRS.moveNext
  17. loop
  18. objRS.update()
Let me know if this makes sense.

Jared
Jan 14 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Nick Calladine | last post by:
Learning : Loop to list all dropdown box values on a form Can some one point me in the right direction : I have a form which I want to loop through I basically want to get all the selected...
4
by: EmmettPower | last post by:
Hi, I have a form which includes a field 'number'. When 'number' is changed additional fields ('item_0', etc) are generated on the form using 'onchange'. I want to validate the form using...
0
by: Mike O. | last post by:
MS Access 2003 "filter by form" has drop down lists that allow the user to select values for each field to filter by. However, once some values are selected,the remaining dropdown lists remain the...
0
by: Henke | last post by:
Hi, I have done some research about my problem I have when using the "back button" in IE to go back to a page with two dropdown lists. The both dropdown lists are populated with data. The...
2
by: Chris Becker | last post by:
This is my attempt to rephrase a question I asked earlier that got no response. I suspect it was my poor/unplanned wording. Here is another attempt: I have a form with some drop down lists. I...
0
by: Boris | last post by:
When I dynamically create CheckButtonList, I add ListItem(s) to my CheckButtonList object chkList chkList.Items.Add(new ListItem("My Text", "My Value")); The resulting HTML doesn't contain...
1
by: Ed Chiu | last post by:
Hi, I have 2 dropdown lists on an ASP.Net page, the first is a list of states of US, the second is City list. When user selects a state, the web page does a postback, create a DB connection and...
2
by: Lair | last post by:
I am creating a page that has three to four databound dropdown lists. Each one is has different data sometimes from the same table but with a different where clause. What is the best way to...
2
by: climberdude | last post by:
I've been working on a site with multiple <select> menus that are generated dynamically with PHP and Javascript and I've been using an AJAX approach. The javascript calls my php page, which hits a...
9
by: Dahak | last post by:
I'm trying to generate dynamic functions to use as separate callbacks for an AJAX API call. The API doesn't seem to allow for the inclusion of any parameters in the callback, so I can't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.