?
Solved

Dynamic Checkboxes and ASP Split Function - can anyone help?

Posted on 2004-04-16
9
Medium Priority
?
1,071 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
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
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 1000 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This video teaches users how to migrate an existing Wordpress website to a new domain.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

762 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