Reputation: 1362
Hi,
I'm creating dynamic input textboxes starting with name: loan0
and also name: balance0
I would like to have classic asp put the values into two arrays
HTML:
<input type="text" name="name" value="John">
<input type="text" name="phone" value="1234567">
<input type="text" name="loan0" value="bla">
<input type="text" name="loan1" value="blabla">
<input type="text" name="balance0" value="test1">
<input type="text" name="balance1" value="test2">
...........and so on...............
Classic ASP:
loan_array = bla,blabla ... and so on..
balance_array = test1,test2 ... and so on...
What I tried:
dim loan_array
loan_array = ""
For Each item In Request.Form
loan_array = loan_array & Request.Form(item) & ","
Next
Response.write loan_array
end if
PROBLEM: The result I get back is all of the values in all of the input textboxes, but I only want those that start with the name balance followed by a number (starting with 0) and loan followed by a number (starting with 0)
THANKS SO MUCH FOR ANY HELP.
Upvotes: 1
Views: 10825
Reputation: 16682
Why do this when ASP can do it for you?
HTML:
<input type="text" name="name" value="John">
<input type="text" name="phone" value="1234567">
<input type="text" name="loan" value="bla">
<input type="text" name="loan" value="blabla">
<input type="text" name="balance" value="test1">
<input type="text" name="balance" value="test2">
<!-- ...........and so on............... -->
Classic ASP:
Response.Write Request.Form("loan")
Output:
bla, blabla
ASP will even build the delimited string based on the order of the form elements with the same name occur.
Based on feedback about handling commas inside the request, I've done a bit of testing myself and the issue is due to how Request.Form("loan")
automatically decodes and collates the entries together but there is a simple way around this and that is to iterate through the instances of Request.Form("loan")
rather then just calling Request.Form("loan")
and having them automatically collated together.
HTML:
<input type="text" name="loan" value="bla">
<input type="text" name="loan" value="blabla">
<input type="text" name="loan" value="test1,test2">
<input type="text" name="loan" value="test3">
<!-- ...........and so on............... -->
Classic ASP:
Dim item
For Each item In Request.Form("loan")
Response.Write item & "<br />"
Next
Output:
bla
blabla
test1,test2
test3
Upvotes: 3
Reputation: 3854
The way you should do it is what Lankymart wrote, i.e. using the same name for all the related fields, and letting ASP do the concatenating-to-string for you. (And then if you want it as an array, you can just do a Split()
on the commas.) However, if you really want to do it yourself, you can concatenate field names in Request.Form
:
N = Request.Form("N") '- number of fields per type
redim loan_array(N) : redim balance_array(N)
For i = 0 to N
loan_array(i) = Request.Form("loan" & i)
'- might as well take advantage of this one-at-a-time approach:
If Not IsNumeric(loan_array(i)) Then
loan_array(i) = 0
Else
loan_array(i) = CLng(loan_array(i))
End If
balance_array(i) = Request.Form("balance" & i)
'- etc.
Next
'- one reason for doing it this way might be that your data has commas in it:
Const delimiter = ";"
loan_list = Join(loan_array,delimiter)
balance_list = Join(balance_array,delimiter)
This counts on there being a known number of fields of each type, but I can't imagine a scenario where you don't know that - if nowhere else, right after generating/writing out your form:
<input type="text" name="loan0" value="<%=loan_array(0)%>">
<input type="text" name="loan1" value="<%=loan_array(1)%>">
<input type="text" name="loan2" value="<%=loan_array(2)%>">
...
<input type="hidden" name="N" value="2">
Upvotes: 1
Reputation: 21
Perhaps this will help.
dim loan_array, myCounter
loan_array = ""
myCounter = 0
For Each item In Request.Form
loan_array = loan_array & Request.Form(item) & myCounter & ", "
myCounter = myCounter+1
Next
Response.write loan_array
Upvotes: -2