Solved

Dynamic Checkboxes and ASP Split Function - can anyone help?

Posted on 2004-04-16
9
1,058 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video teaches users how to migrate an existing Wordpress website to a new domain.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now