cookies and session variables

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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!

Re: cookies and session variables

Japhy wrote:
Quoted text here. Click to load it
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.


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 :

(set_cookie being a javascript function :
// set_cookie(name, value [,expires, path, domain, secure])
// Set cookie value.
function set_cookie (name,value,expires,path,domain,secure) {
        document.cookie = name + "=" + escape (value) +
                ((expires) ? "; expires=" + expires.toGMTString() : "")
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "");
I found on the internet.
Can I set session variables 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? 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!

Re: cookies and session variables

Japhy wrote:
Quoted text here. Click to load it

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.

Quoted text here. Click to load it

Yes.  You can use the value of your submit button for this.

Quoted text here. Click to load it


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.  

Re: cookies and session variables

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

Re: cookies and session variables

Japhy wrote:
Quoted text here. Click to load it
$_POST[<button name>] = <button value>

Where name and value are attributes of the button input.


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.

Site Timeline