Solved

HTML Form submit and store data using cookies

Posted on 2008-10-21
9
4,417 Views
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 www.buildmethebest.com/intel_customdesktop.php

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:
FILL OUT FORM -> SUBMIT -> DISPLAY DATA -> CONFIRM/ADD TO CART -> HAVE IT DISPLAYED AS IN THEIR SHOPPING CART -> CHECKOUT

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!
0
Comment
Question by:Doomtomb
  • 4
  • 4
9 Comments
 
LVL 7

Expert Comment

by:tg_wilk
Comment Utility
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.
<?php

//this has to be the first line in every script that uses session - no newlines, no spaces

session_start();

?>

<!-- you know how to submit to a page so after that: -->

<!-- display submitted data -->
 

<?php

  $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">

</form>
 
 

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

0
 
LVL 44

Expert Comment

by:scrathcyboy
Comment Utility
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
0
 

Author Comment

by:Doomtomb
Comment Utility
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.
0
 

Author Comment

by:Doomtomb
Comment Utility
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.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 44

Accepted Solution

by:
scrathcyboy earned 500 total points
Comment Utility
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.
0
 

Author Comment

by:Doomtomb
Comment Utility
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?
0
 
LVL 44

Expert Comment

by:scrathcyboy
Comment Utility
"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.
0
 
LVL 44

Expert Comment

by:scrathcyboy
Comment Utility
sorry -- "another page and DECODE it exactly as you stored it. "
0
 

Author Comment

by:Doomtomb
Comment Utility
Alright, thanks for getting back to me on that.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now