View Full Version : html shopping cart - Forms

10-01-2006, 08:17 AM
Hi everyone,

Hi, i'm Jackie and i'm new to the HTML scene. I am writing in need of some help as I have to write a basic "shopping cart" for an assignment.

I have done some research and have begun writing simple html code. Essentially i have a form where i enter the quantity i want for ITEM X which cost say $1. When i push submit it goes to a CGI script which i use the quantity to determine how many the customer wants. Now, how do I obtain the price for ITEM X? Is it possible to, on submit, to send the quantity information (user input) and the price (fixed) to the CGI script?

Here's the code part i'm talking about:
<b>Book 1 Cost:</b> $1
<form name="input" action="/cgi-bin/add_to_cart.cgi" method="get">
Please enter quantity:
<input type="text" name="Item1Q">
<input type="submit" value="Add to Cart">

You see when i go in the cgi script:

my $itemQuantity = param('Item1Q");

$itemQuantity would contain the quantity from html code correct? is it possible to pass, say a nother variable, i.e. the price of the book?

Thanks for any help :) :) ,

10-01-2006, 08:33 AM
You could use a hidden input field eg. <input type="hidden" value="5.00">

Not advisable for a real cart though unless the server side script validates that the correct price is passed as it is trivial for someone to copy the form and change a hidden price to 0.01

10-01-2006, 08:41 AM
Hey felgall

Thanks for ur reply. By servier side script do u mean, say a CGI script that queries a database which would have the price of the book to compare it with?


10-01-2006, 09:48 AM
I second that motion. The only reason for providing the price in the form would be to use it in actual calculations. This is clearly not advised for the reasons mentioned by felgall. And yes, he means you would take the price that was sent to the script and validate it against the price stored for the given item number, however, this just complicates matters when you should be pulling the price by id anyway, without the need for comparison.

This assumes you have a database of products, if you dont...are you using flat files? Hard coding these pages? If you're hardcoding or using flat files, it's highly advised that you start learning database programming immediately - once you understand how to do it correctly, it can cut down your development time significantly.

10-01-2006, 02:53 PM
Thanks for your reply syosoft!

Yes I realised i would need a database of products. I'm okay with databases so that shouldn't be a problem. I've read through the textbook and i'm having problems just thinking how ALL of these things (PERL/CGI, HTML, MySQL, Apache) would come together.

U see i have a form field of "quantity" and when i put, say quantity of 3, i pass that into a cgi module that i'm writing, and the cgi would have to query the dB of products, find the correct product and "add" it to a cart.

What i havn't thought of is where to store the cart information (i.e. the number and type of products). Have you guys got any suggestions?


EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum