Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Dynamic Checkboxes and ASP Split Function - can anyone help?

Posted on 2004-04-16
9
1,067 Views
Last Modified: 2012-05-04
Hi there,
I have a form which contains a series of checkboxes. All the checkboxes have the same name but carry different values. The checkboxes are created dynamically and their values are taken from a table in a database - the number of checkboxes shown in the form relates to the number of entries in the table column in the database.

When the user selects any number of checkboxes and submits the form their values are then added inside another table as a comma delimated string (apples, pears, oranges, etc).

On editing this form I have looped through the initial number of checkboxes for the form with no problems:

<% Do While Not rsMat.EOF%>

<input type="checkbox" name="ProdMat" value="<%=rsMat.Fields("rbFruit").Value%>"><%=rsMat.Fields("rbFruit").Value%>

<%
rsMat.MoveNext
Loop
%>

but then I need to have checked the boxes which were saved by the user, and this is where I am stuck.

I have a feeling that the Split() function comes into this solution but am unsure how this would integrate so that it will know which boxes should be ticked when it loops through it.

I know this can be done by passing values between two <SELECT> boxes but in this instance I'd rather use checkboxes if pos.

Many thanks in advance.
Mike
0
Comment
Question by:HairJam
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 28

Expert Comment

by:peakpeak
ID: 10844564
In my opinion VBScript is ugly (my opinion again) ... here's how I do it in JScript ...

fn = Request.Form("fieldnames");      // Get the field names in the form
fieldnames = fn.Split(",");            // Convert to an array
for (var field in fieldnames)            // Loop through all fields in the form
{
    fieldval = Request.Form(field)
 ... do take care of the value ...
 ... etc  ..
}

Regards
Peter

0
 
LVL 6

Expert Comment

by:sforcier
ID: 10844734
The way I typically handle this situation is as follows:

<%
While Not(rs.EOF)
%>

   <input type="checkbox" name="ProdMat<%=rs("Primary_Key")%>" value="<%=rsMat.Fields("rbFruit").Value%>"

<%
If Request.Form("ProdMat" & rs("Primary_Key") <> "" Then
   Response.Write "checked"
End If
%>      

><%=rsMat.Fields("rbFruit").Value%>

<%
   rs.MoveNext()
Wend
%>

When processing the results you'll have the primary key available for each checkbox (with only a little string parsing necessary).
0
 
LVL 28

Expert Comment

by:peakpeak
ID: 10844867
(just a comment) Strange handling .... it's so NEAT in JScript ....
/P
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 6

Expert Comment

by:sforcier
ID: 10845376
Just because I feel I have to vindicate VBScript for some reason. To do the code you posted in VBScript, it would be:

<%
fieldnames = Split(Request.Form("ProdMat"), ",")
For i = 0 To UBound(fieldnames)
   fieldval = fieldnames(i)
'take care of the value ...
'etc
Next
%>

That's the same amount of code.

The code I posted before is a way to retain the information that the user had checked off on the form after the page is submitted.
0
 
LVL 28

Expert Comment

by:peakpeak
ID: 10845623
sforcier: Ok, what is your point? Not letting me down to "compress" code, what does that help HairJam?
/P
0
 
LVL 6

Accepted Solution

by:
Lord_McFly earned 250 total points
ID: 10848548
When you are listing your fruit use the InStr(start,string1,string2) to see if the current fruit - sMat.Fields("rbFruit").Value for example is in your comma separated value that you have stored.

<%
'Use As Example rsList("MyFruitList") // "Apple,Pear,Orange,Plum"

Do while not rsMat.EOF%>
      <input type="checkbox" name="ProdMat" <%If InStr(1,rsMat.Fields("rbFruit").Value,rsList("MyFruitList")) then%>checked<%End If%> value="<%=rsMat.Fields("rbFruit").Value%>"><%=rsMat.Fields("rbFruit").Value%>
      <%rsMat.MoveNext
Loop
%>

0
 
LVL 6

Expert Comment

by:sforcier
ID: 10848767
peakpeak: I was just showing you that the code you wrote in JavaScript could be just as *neat* in VBScript. I'm not tryin' to start a flamewar. ;)

My second post processes the submitted information (without making any changes to the originally posted code) while the first post retains the selections by re-implementing the original code. I figure there's value in both posts, and since the original post is in VBScript there's a good chance that HairJam prefers VBScript.

HairJam: Depending on your needs, Lord McFly's code will probably require the least amount of effort on your part. Unless you stand to benefit from having the checkboxes be named differently (which I personally like, because I find it easier to process... just my personal opinion), you should probably try Lord's code first.
0
 
LVL 6

Expert Comment

by:Lord_McFly
ID: 10848815
sforcier :)

When dealing with a single record as in the example its quite an efficient way to store multiple checkbox results, I tend to do it quite a bit with both checkboxes and radio button - storing strings of "1 0 1 1 0" & "[Peter][Pete]" (I encase in [] so when I do the InStr() I would check '[Pete]' and not 'Pete' because there are 2 Pete's in the string) for example which I can later Split() up or do as in my example.

However when listing multiple records I do also index my form items with a counter so I get Box_1, Box_2 etc.

Anyway - I'll stop babbling now :)
0
 

Author Comment

by:HairJam
ID: 10864376
Many thanks for all your posts. Lord McFly's post was actually the quickest for me to implement and works just great (as you correctly pointed out 'sforcier').

I actually didn't think of the approach of independantly named checkboxes as per your initial suggestion sforcier - not sure why - but it was appreciated nonetheless.

I tend to use a mixture of VBScript and Javascript in my coding but tend to favour VBScript by preference - I think that may purely be because I'm not as confident with my Javascript.

Thanks all!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

860 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