HTML Form submit and store data using cookies

Posted on 2008-10-21
Last Modified: 2012-05-05
It's really easy to find websites that will show you how to code a form and all its little inputs using HTML. The only thing is what actually happens when the user clicks "Submit." My site is

I have a form all coded (right now there isn't a submit button, but that's not hard to do). The form is for a custom computer. I have radio buttons and checkboxes. I need the form to Submit the data and go to another page and display the information the user has selected from the form and then another textarea for them to optionally add any notes to the order. This page is to clearly display everything the user has selected and for them to confirm it. If they wish, I would like for them to be able to confirm it or go back and make changes if necessary. I then want them to click another button from the confirmation page and have the order stored in their "Shopping Cart" where they then can browse the rest of the site. Then when they are ready to "Checkout", ask the user for some shipping info, billing info etc. and then submit the data to a mySQL database. I know how to do the checkout page and submit to mySQL databases.

My question is how to store this information temporarily and display it later (like on the confirmation page). Also if it is possible, to allow the user to make changes to the order once it is in their "cart" after they have confirmed it and added it to their cart.

So, again, if this is confusing:

I know this is alot to ask but I don't think it's anything out of the ordinary for an ecommerce site. Reason I'm doing this myself instead of like ZenCart is because of the custom order form.

I appreciate any help! Thank you!
Question by:Doomtomb
  • 4
  • 4

Expert Comment

ID: 22772813
The easiest way to do it is to store your data (cart in this example) in your session ($_SESSION in php). Session data is stored on the server throughout the visit of a user and is remembered from page to page.

You need to start your session before anything is echoed from the script. A simple example based on one field can be found below. Bear in mind that it's a very simplified version.
//this has to be the first line in every script that uses session - no newlines, no spaces
<!-- you know how to submit to a page so after that: -->
<!-- display submitted data -->
  $product = isset($_POST['product'])?$_POST['product']:'none';
<form action="confirm.php">
Name of the product: <?php echo $product; ?> 
<input type="hidden" name="product" value="<?php echo addslashes($product); ?>"> <!-- we store the info in the hidden field untill it's confirmed -->
<input type="submit" name="confirm" value="Confirm">
<!-- add to cart in confirm.php -->
if (isset($_POST['confirm']) && isset($_POST['product']))
  $product_info = array("name"=>$_POST['product']); //pricing, amount and so on
  $_SESSION['cart'][] = $product_info;
<!-- display shopping cart wherever -->
if (isset($_SESSION['cart']))
  foreach ($_SESSION['cart'] as $product_info)
    echo $product_info['name']."<br>";
<!-- the same would be for the checkout, but instead of echoing - add prices and amounts and echo the total -->

Open in new window

LVL 44

Expert Comment

ID: 22772974
Any well designed custom shopping cart is better than canned ones, so you should STICK WITH that goal.   What you are asking for here is developing a full shopping cart experience, and I am sure you realize that it is beyond the scope of EE to give you all this.  But here are some pointers --

1.  PHP sessions are great (see above comment) if you want your user's cart memory to last typically less than 8 hours, it is rare to have sessions last longer than that, yes it can be done, but burdens the server.

2.  If you foresee the user wanting to setup a sys config. and think about it for a few days, then you should definitely go with COOKIES that can be read by javascript.  THe only downside to cookies is that some users may have them disabled, but if they do, they cannot really shop anywhere else on the internet either!

3.  So if you want to use cookies, the logic is this --
(1) present shopping page with checkboxes, total etc.   Have continue, checkout, view cart, etc
(2) on clicking a checkout button or view cart, etc, have JAVASCRIPT acting on the button to save the user data to a cookie -- this way it is saved before they proceed.
(3)  When they reach the summary page, if they want to go back, send them back to the same page they just left, so if they make changes, the same javascript will overwrite the cookie with the NEW config.
(4)  Once they have made the purchase, set another cookie, that this transaction is completed.

(5) Now, when they revisit the site, the page needs to READ any existing cookies that have been saved.  If it finds the setup config cookie, and NO completion cookie, the user is presented with the last order -- hey here is your last setup config -- do you want to continue?

(6) if the completed cookies if found, this can be put on the page as history -- view last configuration.

The cookie code is well done by Bill Dortch -- just search for his name with cookie code in google.  Doing the cookies is a battle the first time, but easy once you have done it.  I use them all the time and they work terrific for what I need -- long term memory of the user for that specific site.

Hope this helps

Author Comment

ID: 22773059
Hey, excellent walkthrough and ideas you two! ^_^

I'm going to give this a shot, it'll probably take me a lil while to setup this up properly (at least a few days, I think, I haven't used this stuff before so I don't know for sure).

In the mean time, I'll keep the question open.
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.


Author Comment

ID: 22773644
I have a question with this. I think I would like to use session to store the values of the order form upon submission, then display the values on the confirmation page. Then once they confirm it, store it in the cart using cookies.

How would I store the values to the SESSION upon submitting the form? I have different categories of the computer like memory, video card, etc. Just a short example would help me greatly.
LVL 44

Accepted Solution

scrathcyboy earned 500 total points
ID: 22773784
you can't cross them.  PHP sessions are server-side items, accessible only by server languages like PHP.

In contrast, cookies that are saved for the user are saved on the user's local hard drive, and are accessible only by LOCAL scripts, specifically javascript.  The two are mutually exclusive.  It is one or the other.

Now people will tell you that you can save user cookies on the server and access them via PHP.  Sure this is possible, but there are serious consequences -- 100,000 visitors all saving cookies to the server, it bogs down your server so much it won't work.  Second, if you are dealing with server cookies, then every time you want to access or save those cookies you need to submit the request to a server script, which slows down page interaction and makes that many more steps on your commerce site that make people wait, and alienate them.

The most efficient is local user cookies accessed via javascript for long term storage, or PHP sessions for short term.  Make a decision.  You can't have both and also have a efficient shopping experience.

Author Comment

ID: 22773817
Ok, I'll use cookies, so how can I save the order form to cookies? Should I have a separate cookie for each category? Like case - choice, lighting - none, memory - 2GB, or is there a more efficient way like saving them all to one cookie via an array?
LVL 44

Expert Comment

ID: 22860153
"Should I have a separate cookie for each category? Like case - choice, lighting - none, memory - 2GB, or is there a more efficient way like saving them all to one cookie via an array?"

You just put everything you want into ONE cookie -- up to a limit of 24KB of information.  If you are going to exceed that, make more than one cookie.  Remember, the cookie records info EXACTLY the way you stored it, so just remember how you stored it, and you can retrieve it by another page and decide it exactly as you stored it.   There is no magic to cookies, just a size limit.  Whatever way to saved it, that's the way you get it back.
LVL 44

Expert Comment

ID: 22860157
sorry -- "another page and DECODE it exactly as you stored it. "

Author Comment

ID: 22860456
Alright, thanks for getting back to me on that.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Html split(text) Part2 6 20
Compute age Html 2 23
PHP Curl Problem 10 31
disable open new tab with right mouse click in <a> tag 3 18
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question