Form structure not staying in session scope

Discussion in 'Programming' started by tbarr60, May 23, 2007.

  1. #1
    I saw something that looked cool in Hal Helm's newsletter and wanted to try it out. He placed a form structure into session scope. It works fine when I put it in but when I return to the page the form data is no longer in the session scope.

    
    <cfset session.previousFormValues = form />
    
    Code (markup):
    The rest of the page has a simple form and some CFDUMPS. Other variables stay in the scope but the form structure does not stay.

    Any thoughts are appreciated.
     
    tbarr60, May 23, 2007 IP
  2. IsRoss()

    IsRoss() Peon

    Messages:
    116
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Interesting idea...I can think of many possible uses!

    I assume you have session variables enabled in your application, you are locking session vars, etc.?
     
    IsRoss(), May 23, 2007 IP
  3. tbarr60

    tbarr60 Notable Member

    Messages:
    3,455
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    210
    #3
    Sessions are enabled, I put CFLOCK in place but it isn't really necessary since I am the only user at the moment.

    I can create an array, say myarray, and set it as session.myarray and it sticks but the form structure doesn't stay in the session scope.

    I think I'll create my own struct and put it in the array and see if it stays.
     
    tbarr60, May 23, 2007 IP
  4. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Yes this is good, it's sort of like putting a function in the session-scope as well.

    Here are few things:

    1) Check your logs (if you can) or
    2) Try using StructInsert

    HTH

    daTropics
     
    datropics, May 28, 2007 IP
  5. tbarr60

    tbarr60 Notable Member

    Messages:
    3,455
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    210
    #5
    Nothing in the logs when I add form data to session scope and return.

    The session scope will retain an array or struct that I create but it won't retain the form data I pass in.
     
    tbarr60, May 30, 2007 IP
  6. IsRoss()

    IsRoss() Peon

    Messages:
    116
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This is almost sounding like a bug...I don't see any reason why this wouldn't work.
     
    IsRoss(), May 30, 2007 IP
  7. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #7
    Make sure on the initial page load of the form that you are not redefining the session = form variable. If you are that means it is working, its just resetting the variables due to your logic and the fact that the Form vars currently contain no data.
     
    drewbe121212, Jun 18, 2007 IP
  8. newbish

    newbish Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    you could copy the form structure to a session variable using something like structcopy. Make sure you lock/unlock correctly though.
     
    newbish, Jun 19, 2007 IP
  9. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #9
    drewBe has a good point, ensure that once written, you aren't overwriting with the no blank form.
     
    datropics, Jun 21, 2007 IP
  10. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    That's because structures are copied by reference. The variable session.previousFormValues only stores a reference to FORM. So anything that happens to FORM also happens to your session variable. If you visit another page where the FORM structure is empty, your session.previousFormValues will also be empty because you're only storing a reference to the FORM structure.

    So newbish is right. You need to make a copy of the structure with either StructCopy or Duplicate. Generally, Duplicate is safer because it makes a deep copy of nested structures.

    
    <cfset session.previousFormValues = Duplicate(form) />
    
    Code (markup):
     
    cfStarlight, Jun 23, 2007 IP