Forum Moderators: open

Message Too Old, No Replies

Request.Form & For Loop

Request.Form & For Loop

         

Squigee

11:24 pm on Mar 20, 2005 (gmt 0)

10+ Year Member



Is there anyway to add a number to the end of a request.form statement in asp?

For instance, let's say on one page I have the following input fields in a form:

<input name="car1" value="">Mercedes</input>
<input name="car2" value="">Ford</input>
<input name="car3" value="">Nissan</input>
<input name="car4" value="">Chevrolet</input>
<input name="car5" value="">Toyota</input>
<input name="car6" value="">Lexus</input>

<input name="home1" value="">Stanford</input>
<input name="home2" value="">Gable</input>
<input name="home3" value="">Clark</input>

The information in between the input tags (i.e. Mercedes) is the information the user inputs in the form.

When the user hits the submit button, it takes them to another page and displays the values they inputted above to the page using response.write. I should mention that the number of each of the groups displayed is dependant upon a number the user enters. So if someone enters 6 cars and 4 homes the above would be displayed. The number of these will change based on a number they enter into a previous page. I keep the values they enter stored so I can use them in a loop to write the information to the page.

how would I go about making a loop that displays the information by adding a number to the end of the request.form? Or if this is not possible, can anyone help me come up with another solution that will not be dependant upon me having to add anything to my form. These were my thoughts that don't work for obvious reasons:

<%
addInt = Request.Form("addNum") //Number stored that user enters
addCar = cint(addInt)

for i = 0 to addCar
Response.write ("<strong>Cars: ") & i + 1 & ("</strong><br>")
Response.write ("Car #: ")
Response.write Request.Form(Car & i)
Response.write ("<br><br>")
Response.write ("<strong>Homes: ") & i + 1 & ("</strong><br>")
Response.write ("Type: ")
Response.write Request.Form(Home & i)
next
end if
%>

I would like the following to be displayed on the page from the code:

Cars
Car #1: Mercedes
Car #2: Ford
Car #3: Nissan
Car #4: Chevrolet
Car #5: Toyota
Car #6: Lexus

Homes:
Type: Stanford
Type: Gable
Type: Clark

I am somewhat new to ASP in terms of coding anything complex. So I realize you may be thinking "of course that wouldn't work!", but I tried several other things and although the above does not work, hopefully it clarifies that I am trying to add a number to the end of Request.form or something that accomplishes the same thing. Thank you and sorry for the lengthy post.

mattglet

12:26 am on Mar 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



for i = 0 to addCar
response.write("Car #" & i &": " & request.form("car" & i)
next

Create another loop for your homes, and it should work the same way.

Squigee

1:52 pm on Mar 21, 2005 (gmt 0)

10+ Year Member



Thank you Matt, worked like a charm! One note, just in case someone else ever has a similiar problem and lands here. I got an internal server error and when I added a closed parenthesis to the end of the response.write statement, it corrected the problem.

for i = 0 to addCar
response.write("Car #" & i &": " & request.form("car" & i))
next

mattglet

6:27 pm on Mar 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, sorry about that... did it up quick ;)