#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    5
    Rep Power
    0

    Loop through form fields to set cookies


    Hi,
    I have a table in which I count the rows, and for each record it finds it uses that number to dynamically name the form field. i.e.

    <input type=text name="FirstName<%=i%>">

    I got that to work just fine. What I'm trying to do now, is post the form, and on the new page
    dynamically create cookies based on the number of form fields there are. For example, my desired end result would look something like this..

    response.cookies("First0")=request.form("FirstName0")
    response.cookies("First1")=request.form("FirstName1")


    and so on.
    Is there a way to do this by looping, instead of hardcoding? I have my feeble attempt below.
    Any help would be appreciated. Thanks.

    <%
    dim getTotal
    getTotal = request.form("theTotal")

    for i = 1 to getTotal
    dim FN
    FN = FN & i
    FN = request.form("FirstName"& i)
    response.cookies("First"& i)= FN
    response.write "<body bgcolor=red>"
    response.write request.cookies("First"& i)

    next



    %>
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    5
    Rep Power
    0

    Question Half Way There


    So, i've partially solved it. I can get it to write the cookie values equal to the form name value:
    i.e.
    response.cookies("First0") = request.form("FirstName0")
    When I write within the loop the data shows up, but when I try to call the cookie from another page, or after the loop is closed
    all the cookies have the most recent value

    dim getTotal
    getTotal = request.form("theTotal")

    dim i, x, FN

    for i = 0 to getTotal
    for each x in Request.Form
    if left(x,9) = "FirstName" then
    response.cookies("First"& i)= request.form(x)
    response.write response.cookies("First" &i)
    ''this outputs it fine


    end if
    next
    next

    ''''when I call the cookies, it does this.

    First1=Tosha
    First2=Tosha
    First0=Tosha

    Thanks a ton for any help you can offer.
  4. #3
  5. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,559
    Rep Power
    4550
    All I can do is point you at the docs

    http://msdn.microsoft.com/library/de..._resoccook.asp
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    5
    Rep Power
    0

    Got It!


    Thanks Doug. Looking at that actually helped me look more closely. I was using unecessary loops in an attempt to create unique cookie names, when in fact, the name of the form fields were unique all by themselves.


    for each x in Request.Form
    if left(x,9) = "FirstName" then
    FN = request.form(x)
    'response.cookies("First"& i ) = FN
    'part of the unecessary loop.

    response.cookies(x) = FN

    this actually outputs correctly when I call the cookies

    FirstName0=Sally
    FirstName1=Bert

    Thanks again.
  8. #5
  9. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,559
    Rep Power
    4550
    I'm glad you got it sorted out.

IMN logo majestic logo threadwatch logo seochat tools logo