Solved

Dynamic Checkboxes and ASP Split Function - can anyone help?

Posted on 2004-04-16
9
1,069 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
[X]
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
  • 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

691 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