Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 20th, 2009, 05:27 AM
Authorized User
 
Join Date: May 2007
Posts: 73
Thanks: 4
Thanked 0 Times in 0 Posts
Default dynamic variable names

Hi
I am requesting values from form and saving in variables iFName1,iFName2,iFName3...iFName5

Now because some of the variables would be empty so checking them within an if statement and if the are not empty then save in variable FName (that later on store in the database). Because my variabe names are same apart from the last number there for i am trying to create the variable name on runtime but for some reason rather then saving the variable value i am getting the actual variable name in the variable Fname, ANY IDEA WHAT I AM DOING WRONG HERE?

many thanks


Dim iRName,iREmail,iFName1,iFEmail1,iFName2,iFEmail2,i FName3,iFEmail3,iFName4,iFEmail4,iFName5,iFEmail5, FName, FEmail

'' Retrive form fields
iRName = trim(request.Form("iRName"))
iREmail = trim(request.Form("iREmail"))
iFName1 = trim(request.Form("iFName1"))
iFEmail1 = trim(request.Form("iFEmail1"))
iFName2 = trim(request.Form("iFName2"))
iFEmail2 = trim(request.Form("iFEmail2"))
iFName3 = trim(request.Form("iFName3"))
iFEmail3 = trim(request.Form("iFEmail3"))
iFName4 = trim(request.Form("iFName4"))
iFEmail4 = trim(request.Form("iFEmail4"))
iFName5 = trim(request.Form("iFName5"))
iFEmail5 = trim(request.Form("iFEmail5"))



Dim i

For i=1 to 5
if iFName & i <> "" AND iFEmail & i <> "" then
FName= iFName & i
FEmail=iFEmail & i

response.Write(FName)
response.Write(FEmail)
end if




'VALUES GOES IN DB HERE



next
 
Old January 20th, 2009, 12:12 PM
Friend of Wrox
 
Join Date: Dec 2006
Posts: 104
Thanks: 9
Thanked 1 Time in 1 Post
Default

Sheraz,

I've had a play with the code and added a few array variables..

Try amending the below code (works) to your needs..

Code:
<%
Dim iFName(5),iFEmail(5)
'' Retrive form fields
iRName = "name"
iREmail = "email@email.com"
iFName(1) = "name1"
iFEmail(1) = "email1@email.com"
iFName(2) =  "name2"
iFEmail(2) =  "email2@email.com"
iFName(3) = "name3"
iFEmail(3) =  "email3@email.com"
iFName(4 )= "name4"
iFEmail(4) =  "email4@email.com"
iFName(5) = "name5"
iFEmail(5) =  "email5@email.com"
 
Dim i
For i=1 to 5
if iFName(i) <> "" and iFEmail(i) <> "" then
response.Write iFName(i) & "." & iFEmail(i) & "<br>"
end if
next
%>
I am sure one of the more senior members can do a neater job but see how you get on.

Cheers

Aspless
 
Old January 20th, 2009, 04:39 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I don't get that, at all. You have a comment that says
Quote:
'' Retrive form fields
but then you don't actually retrieve any form fields, at all.

HUH?
 
Old January 20th, 2009, 04:46 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

ASPLess has the right idea: Use an array.

But you complicated your own life by naming your form fields INCONSISTENTLY. Some of your form field names have a number on the end ("iFName1") and some don't ("ifName").

Okay, we can use your form names, but it would be easier and more consistent if you'd put a number on EVERY ONE of them.
Code:
Dim iRName(5), iREmail(5)
For i = 0 To 5
    If i = 0 Then prefix = "" Else prefix = i 
    iRName(i) = trim(request.Form("iRName" & prefix))
    iREmail(i) = trim(request.Form("iREmail" & prefix))
Next

' and then you can do
For i = 0 To 5
     If iRName(i) <> "" And iREmail(i) <> "" Then
         Response.Write iRName(i) & "::" & iREmail(i) & "<p>" & vbNewLine
     End If
Next
The Following User Says Thank You to Old Pedant For This Useful Post:
Sheraz Khan (January 21st, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
variable field names philip r VB How-To 2 January 11th, 2009 08:46 PM
generate variable names dinamycally gcorcuera Classic ASP Basics 7 May 21st, 2007 09:53 AM
dynamic variable/object names TheBFJ Excel VBA 2 November 13th, 2006 07:11 AM
Problem with variable names shenku Javascript 2 May 6th, 2005 03:46 PM
Retrieving Dynamic Field Names tdaustin Classic ASP Basics 4 January 12th, 2004 11:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.