cookies and session variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Japhy

    #1

    cookies and session variables

    Hello,
    I need to save values typed into forms across several scripts, before
    the user actually 'submits' the form to update the mysql db.

    I have been exploring ways to do this, and looking for some guidance as
    to whether to use
    cookies or PHP session variables.

    Should my use of either be conservative due to performance reasons?
    What are the advantages of either?

    To briefly describe what I am trying to do:
    I have 3 forms : formA, formB and formC
    each has about 15 entry fields (from the same mysql table).
    formA has fieldA1, fieldA2, etc
    formB has fieldB1, fieldB2, etc
    formC has fieldC1, fieldC2, etc

    The user can navigate from formA to formB, etc with a button click.


    Right now, I am populating each form from the mysql db value. But I
    need to save values typed in while the user navigates from page to
    page. Eventually, the user submits and I update the db from all forms.
    The easiest way for me to incorporate this into my existing logic is
    to, before going into formA, set a cookie (or session variable) for
    each field. If they go to formA and change a value, reset the cookie
    (I am doing this with an onblur call to javascript function.) Seems
    inefficient for me to set a cookie for every field...they may only
    change 1 field...or none.
    But I am having trouble grasping any other way to do it....Thus, my
    question about efficiencies, performance, etc.
    Thanks for any advice!

  • Ian

    #2
    Re: cookies and session variables

    Japhy wrote:[color=blue]
    > Hello,
    > I need to save values typed into forms across several scripts, before
    > the user actually 'submits' the form to update the mysql db.
    >
    > I have been exploring ways to do this, and looking for some guidance as
    > to whether to use
    > cookies or PHP session variables.
    >[/color]
    I'd use sessions for this, otherwise the user's data is being send back
    and forwards in each HTTP request and response. Even with a secure
    connection, you may also end up with potentially sensitive data left in
    the plain test browser cookie file.

    I keep an array of form inputs in my session and import them into each
    form as it is generated, so common fields between forms get filled in
    when the user moves on to a new form.

    Ian

    Comment

    • Japhy

      #3
      Re: cookies and session variables

      Thanks for the reply. What you say makes sense. Right now I am using
      code in the <input for each field :
      onblur="JAVASCR IPT:set_cookie( 'mycookie',docu ment.forms0].FieldA.value)"

      (set_cookie being a javascript function :
      // set_cookie(name , value [,expires, path, domain, secure])
      // Set cookie value.
      function set_cookie (name,value,exp ires,path,domai n,secure) {
      document.cookie = name + "=" + escape (value) +
      ((expires) ? "; expires=" + expires.toGMTSt ring() : "")
      +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
      }
      I found on the internet.
      Can I set session variables likewise...in a javascript function? I
      have tried to find the right syntax, and so far no luck. My other idea
      was to POST when they page from 1 page to another to collect the
      values, but I am already POSTING on the form to handle the validation
      and input of data.
      Would you suggest :
      1) running some kind of JS function onblur to set the session
      variables?...li ke I'm doing now with cookies.
      2) putting some kind of logic in my form handling script to distinguish
      if they are paging from 1 to another page, or 'submitting' the form?
      3) something entirely different
      Thanks again!

      Comment

      • Ian

        #4
        Re: cookies and session variables

        Japhy wrote:[color=blue]
        > Thanks for the reply. What you say makes sense. Right now I am using
        > code in the <input for each field :
        > onblur="JAVASCR IPT:set_cookie( 'mycookie',docu ment.forms0].FieldA.value)"
        >
        > Can I set session variables likewise...in a javascript function?[/color]

        Sessions are purely server side.

        Use session_start() to create one and the $_SESSION super global to
        store your data (which can be harvested from $_POST) between pages. No
        client side code is required.

        [color=blue]
        > Would you suggest :
        > 1) running some kind of JS function onblur to set the session
        > variables?...li ke I'm doing now with cookies.[/color]
        Nope.
        [color=blue]
        > 2) putting some kind of logic in my form handling script to distinguish
        > if they are paging from 1 to another page, or 'submitting' the form?[/color]
        Yes. You can use the value of your submit button for this.
        [color=blue]
        > 3) something entirely different[/color]
        Maybe....

        Ian

        Comment

        • Japhy

          #5
          Re: cookies and session variables

          Thanks for your advice. I'll look this up, but in case I can't find
          it....How do I get the value of the submit
          button in my form handling script. IE : FormA.php, when submitted,
          posts to FormHandler.php (this is where I do my
          validation and DB insert or set.
          TIA

          Comment

          • Japhy

            #6
            Re: cookies and session variables

            Hello, I think I got it $_POST[submit]...thanks again

            Comment

            • Ian

              #7
              Re: cookies and session variables

              Japhy wrote:[color=blue]
              > Thanks for your advice. I'll look this up, but in case I can't find
              > it....How do I get the value of the submit
              > button in my form handling script. IE : FormA.php, when submitted,
              > posts to FormHandler.php (this is where I do my
              > validation and DB insert or set.
              > TIA
              >[/color]
              $_POST[<button name>] = <button value>

              Where name and value are attributes of the button input.

              Ian

              Comment

              • Japhy

                #8
                Re: cookies and session variables

                Thanks again, Ian, for your advice.
                This seems like it is going to solve
                my problem (easier than I thought, too)
                Couldn't have done it without you!
                Much appreciated.

                Comment

                Working...