Question

Dynamic Form Problem / Session Variables!

Asked by: SimonWGoldsmith

hello folks, here goes......

I have a form on an asp page and within that form there is a repeat region that loops through a recordset. The code for this is below.

<%
While ((Repeat1__numRows <> 0) AND (NOT rsRegionAsscociation.EOF))
If Repeat1__numRows mod 2 Then
      strBGColor = "#e8e8e8"
Else
      strBGColor = "#FFFFFF"
End If
%>
          <tr>
            <td align="right" valign="top" class="td_darkblue">&nbsp;</td>
            <td>&nbsp;</td>
            <td align="center" bgcolor="<%=strBGColor%>"><input type="checkbox" name="<%=(rsRegionAsscociation.Fields.Item("RegionName").Value)%>" value="1"></td>
            <td bgcolor="<%=strBGColor%>"><%=(rsRegionAsscociation.Fields.Item("RegionName").Value)%></td>
          </tr>
          <%
  Repeat1__index=Repeat1__index+1
  Repeat1__numRows=Repeat1__numRows-1
  rsRegionAsscociation.MoveNext()
Wend
%>

As you can see from the code above I set the name of the checkboxes dynamically using the name of a recordset value on each loop through the recordset.

The form targets another page where Session Variables are set for ALL form fields.

What I need is to dynamically create SINGLE Session Variables for each dyanmic checkbox. So for each dynamic checkbox that is checked I want to set a session variable for it with a value of 1, the session variable has to have the same name as checkbox.

I need it to work like this so that no matter how many dynamically created checkboxes there are in the form, there is always a corresponding session variable for it.

12 dyanmic checkboxes = 12 session variables

anyone have any ideas? :)

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-10-10 at 03:46:27ID20762863
Tags

asp

,

form

Topic

Active Server Pages (ASP)

Participating Experts
3
Points
125
Comments
17

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. recordsets
    I'm not really sure how a recordset works. I want to retreive the data from a database and populate text boxes with the fields from the recordset. This I have been able to do. If the user changes anything and clicks update I want the recordset to update the information. I'...
  2. Need help with nested recordsets and loops logic
    I have an internal web page running to keep track of the status of the products & images on our web site. It's a series of recordsets with while/wend's each pulling data from the previous to establish the new recordset. Basically it's: rs1 - distinct(masterCode) rs1 while...
  3. Looping through a recordset
    I have a recordset that chooses only the sport for a menu box based on certain conditions, <% Dim rsSport__varsign rsSport__varsign = "1" If (rsAlready.Fields.Item("ksport_id").Value <> "") Then rsSport__varsign = rsAlready.Fields.I...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: SimonWGoldsmithPosted on 2003-10-10 at 03:53:29ID: 9526695

I think the code I need for the target page is something like what I have already written below, but it contains syntax errors I guess cos of the punctuation.

<%
While Not rsRegionAssociation.EOF
strRegionName = (rsRegionAssociation("RegionName"))
Session("<%=strRegionName%>") = Request.Form("<%=strRegionName%>")
response.Write Session("<%=strRegionName%>")
rsRegionAssociation.MoveNext()
Wend
%>

 

by: GaryC123Posted on 2003-10-10 at 04:08:23ID: 9526752

I'd rename your checkbox
<input type="checkbox" name="chk_<%=(rsRegionAsscociation.Fields.Item("RegionName").Value)%>" value="1">

Then on the next page

for each x in request.form
   if left(x.name,4)="chk_" then
      session(mid(x.name,5)) = request(x)
   end if
next

 

by: SimonWGoldsmithPosted on 2003-10-10 at 04:24:09ID: 9526831

whats the x stand for, or what should it stand for in my code?

Can you give an example using the fields and names I have given, if possible please.

 

by: kblack15217Posted on 2003-10-10 at 04:30:16ID: 9526863

What Gary said is a good idea.  The X is just a temporary variable used to iterate through the form collection.  In this case, X will eventually assume the identity of all of your checkboxes from the previous page.   The code Gary has provided should be on your processing page, and is the most efficient way of discovering your dynamic checkboxes ( short of actually using a recordset again to get the field values).   You should not prefix any other form elements with chk_.

This being said, do you have any other design options?   Session variables are bad enough as is, but an unknown number could really cause you problems down the road.  Besides, how will other programs in your web be aware of which Session variables exist if they are always dynamic?   You may want to consider a more efficient design if possible.   If you must do it this way, I cant think of a better way  then what Gary said.

 

by: GaryC123Posted on 2003-10-10 at 04:33:46ID: 9526882

The x stands for nothing its a kinda shorthand way of referencing the previous form elements. (I made an error with using x.name)
Try this simple example

<form method=post action=ee44.asp>
Box 1 <input type=checkbox name=chk_box1 value=1>
Box 2 <input type=checkbox name=chk_box2 value=2>
Box 3 <input type=checkbox name=chk_box3 value=3>
<input type=submit>
</form>

ee44.asp
<%
for each x in request.form
   if left(x,4)="chk_" then
      session(mid(x,5)) = request(x)
   end if
next
response.write "Box1 = " & session("box1") & "<BR>"
response.write "Box2 = " & session("box2") & "<BR>"
response.write "Box3 = " & session("box3") & "<BR>"
%>

Which ever checkboxes you select they will be placed into a session variable and printed on the next page.

 

by: SimonWGoldsmithPosted on 2003-10-10 at 04:47:23ID: 9526939

Is there an easier way for me to do this than using session variables then. I open to sugegstion.

session variables were used origionally due to the form covering 3 pages.

If theres a simpler way of doing this then I am willing to alter the design.

 

by: gladxmlPosted on 2003-10-10 at 04:48:36ID: 9526944

SimonWGoldsmith ,

Try this approach...

Create a dynamic checkboxes with the same name but the value will  the actual recordset

<input type="checkbox" name="chkbox" value="<%=(rsRegionAsscociation.Fields.Item("RegionName").Value)%>">

try to check out the link of rmore info...

http://oldlook.experts-exchange.com/Web/Web_Languages/ASP/Q_20450556.html

Or if you just one session value then you can try this....

Now what you just need to do is to session this value...

like for example

session("chkbox") = request.form("chkbox")

If you want to retrieve the values of the session... since making the checkbox the same will make it a comma seperated string

Then you will just manipulate using split function... like this


arraysesssion  = split(session("chkbox")

for i=Lbound(arraysession) to Ubound(arraysession)

response.write arraysession(i)

next

This way you will only deal and track one session variables...

HTH...

HAppy programming...

 

by: gladxmlPosted on 2003-10-10 at 04:58:45ID: 9526996

OOPS some typo....

<%
arraysesssion  = split(session("chkbox"))

for i=Lbound(arraysession) to Ubound(arraysession)

response.write arraysession(i)

next
%>

Happy programming...

 

by: kblack15217Posted on 2003-10-10 at 05:02:14ID: 9527021

As for not using session variables for a three page form, take Gary's code:

for each x in request.form
   if left(x,4)="chk_" then
      Response.Write "<input name='" & X & "' value = '" & request(x) & "'>"
   end if
next

These will make hidden form variables that will be passed to the third page, and can then be accessed again using Garys Loop

 

by: GaryC123Posted on 2003-10-10 at 05:05:39ID: 9527043

Good addition kblack

 

by: gladxmlPosted on 2003-10-10 at 05:06:10ID: 9527048

SimonWGoldsmith,

I think the approach that I had given to you will work...

If you will pass the checkbox data into another form... then you can  put in a hidden field... Using split fucntion you can seperate the data...

Now when you process the data on the 3rd form you will again put it in a hidden field to again manipulate the data using split function just like I had mention...

I wish I could have more time to spare but I got to go... need to sleep...

Hope this help...

Happy programming...

 

by: SimonWGoldsmithPosted on 2003-10-10 at 06:16:22ID: 9527436

Right I now have the following and session variables are getting set for each checkbox in the form.

'Region Association rules 1
session("RegionAssociation") = request.form("RegionAssociation")
arrayRegions  = split(session("RegionAssociation"), ", ")
for i=Lbound(arrayRegions) to Ubound(arrayRegions)
session("Region"&i) = arrayRegions(i)
next

What will the session variables be called on another page???
How do I reference them, I'm abit confusedas to what the names fo the session variables will be. :(

Will the session variables be called Session("Region1") - Session("Region14") say?

 

by: SimonWGoldsmithPosted on 2003-10-10 at 06:48:05ID: 9527656

I cant get my head round this what so ever. lol

 

by: GaryC123Posted on 2003-10-10 at 06:52:23ID: 9527690

Stick with my original solution and kblacks amendment

Place this on pages 2 and 3
for each x in request.form
   if left(x,4)="chk_" then
      Response.Write "<input name='" & X & "' value = '" & request(x) & "'>"
   end if
next

Then on the final page where you do whatever with the variables you just use an amendment of the code

for each x in request.form
   if left(x,4)="chk_" then
      ' What do you want to do now with all the variables.
   end if
next

 

by: kblack15217Posted on 2003-10-10 at 10:24:11ID: 9529119

Let me amend the page 2 code, we forgot to make them hidden:

or each x in request.form
   if left(x,4)="chk_" then
      Response.Write "<input name='" & X & "' value = '" & request(x) & "' type='hidden'>"
   end if
next

 

by: gladxmlPosted on 2003-10-10 at 18:30:39ID: 9531199

SimonWGoldsmith,

Below are three fiel to simulate my approach...
just cut ans paste

=======save this as SimonWGoldsmith-1.asp
<%
'Since the data  is comming form the db
' I will just create an array for the data to be out in the check box

teststr = "checkbox1,checkbox2,checkbox3,checkbox4,checkbox5,checkbox6,checkbox7,checkbox8,checkbox9,checkbox10,checkbox11,checkbox12"

%>

<form name="form1" method="post" action="SimonWGoldsmith-2.asp">
  <table width="75%" border="0">
    <tr>
      <td>Select</td>
      <td> Description</td>
    </tr>
    <%
  arrayteststr = split(teststr,",")
 
  for i = Lbound(arrayteststr) to Ubound(arrayteststr)
 
  %>
    <tr>
      <td><input name="chkbox" type="checkbox" value="<%=arrayteststr(i)%>"></td>
      <td><%=arrayteststr(i)%></td>
    </tr>
    <%next%>
    <tr>
      <td colspan="2"><input type="submit" name="Submit" value="Submit"></td>
    </tr>
  </table>
</form>


========save this as SimonWGoldsmith-2.asp

<%session("chkbox") = request.form("chkbox")%>

<form name="form1" method="post" action="SimonWGoldsmith-3.asp">
<input name="chkbox" type="hidden" value="<%=request.form("chkbox")%>">
Below is the output using the request("chckbox")
1. using the .count property
  <table width="75%" border="0">
    <tr>
      <td>Select checkbox</td>
    </tr>
    <%  
  for i = 1 to request.form("chkbox").count
 
  %>
    <tr>
      <td><%=request.form("chkbox")(i)%></td>
    </tr>
    <%next%>
  </table>
  <br>
  <br>
  2. using the request collection and gettin the elemname
  <table width="75%" border="0">
    <tr>
      <td>Select checkbox</td>
    </tr>
    <%  
  for each elem in request.form("chkbox")
 
  %>
    <tr>
      <td><%=elem%></td>
    </tr>
    <%next%>
  </table>
  <br>
  <br>
  3. using the session
 
 
  <table width="75%" border="0">
    <tr>
      <td>Select checkbox</td>
    </tr>
    <%  
      arraysession = split(session("chkbox"),",")
  for i = Lbound(arraysession) to Ubound(arraysession)
 
  %>
    <tr>
      <td><%=arraysession(i)%></td>
    </tr>
    <%next%>
  </table>
  <br>
  <br>
    <tr>
      <td><input type="submit" name="Submit" value="Submit"></td>
    </tr>
</form>


=======SimonWGoldsmith-3.asp

<%
if request.form("test") <> "" then

response.write "Below are the selected checkbox using session <br>"

      arraysession = split(session("chkbox"),",")
  for i = Lbound(arraysession) to Ubound(arraysession)
 
  response.write arraysession(i) & "<br>"
      
  next

response.write "Below are the selected checkbox using hidden field <br>"

      arrayhidden = split(request.form("chkbox"),",")
  for i = Lbound(arrayhidden) to Ubound(arrayhidden)
 
  response.write arrayhidden(i) & "<br>"
      
  next

else

response.write "Please input a char on the textbox to see the output"

end if
%>
<br><br>

if you will use rin another form then you will just the name of the hidden field...

Since the hidden field is comma delimited as well as the session..<br>

You can use split which is used on the third solution...<br>

But if you still need to pass it on a form just put agin it in a hidden field...

<form name="form1" method="post" action="SimonWGoldsmith-3.asp">
<input name="chkbox" type="hidden" value="<%=request.form("chkbox")%>">
  <table width="75%" border="0">
    <tr>
      <td>Name</td>
      <td><input name="test" type="text"></td>
    </tr>
      <td colspan="2"><input type="submit" name="Submit" value="Submit"></td>
    </tr>
  </table>
</form>

========

HTH...

HAppy programming...




 

by: GaryC123Posted on 2003-11-15 at 19:00:50ID: 9757439

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split Points kblack / GaryC123

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
GaryC123
EE Cleanup Volunteer

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...