Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

product page with checkboxes, tick to customise and add to cost

Status
Not open for further replies.

sonicsnail

Technical User
Nov 10, 2001
48
IN
Hi all,

I have been asked to create a page that should contain the specs of a PC system for sale. Below the specs should be some "optional extras" (CD burners, RAM, etc) which can be selected by the user.

The (simple??!) idea is to have a "Total cost" at the bottom of the page, and when ticks are added beside the optionals, the Total should rise.

What I know:

The host doesn't support anything serverside, so this is a job for good'ol JS. (maybe use a cgi script to email the form results?)
I'd like to use objects/arrays so the Optional Extras can be document.written dynamically and updated by the site owner.
Each array object would have (at least) two values - NAME and PRICE.
I can't find an example of this script anywhere! (Dell use this functionallity, but using page-refreshes)

What I don't know:

How to "subtract" when a tickbox is unticked (I've been thinking of using OnClick to add the value)
The search term to use to find help online!!

I'd appreciate any guidance... someone MUST have done this kind of thing before!

cheers,

Pete
 
Here's a basic solution....

<script>
function updatePrice(){
ttlPrice = parseFloat(document.getElementById(&quot;basePrice&quot;).innerText)

allChecks = document.getElementsByTagName(&quot;input&quot;)

for (x=0; x<allChecks.length; x++){
if (allChecks[x].type == &quot;checkbox&quot;){
if (allChecks[x].checked){
thisBox = allChecks[x].name
newPrice = parseFloat(document.getElementById(&quot;pr&quot; + thisBox).innerText)
ttlPrice += newPrice
}
}
}
document.myForm.ttl.value = ttlPrice
}
</script>

<body onLoad=&quot;updatePrice()&quot;>

<form name=&quot;myForm&quot;>
Basic System $<span id=&quot;basePrice&quot;>199.99</span>
<P><input type=checkbox name=&quot;1&quot; id=&quot;1&quot; onClick=&quot;updatePrice()&quot;>CD Burner - $<span id=&quot;pr1&quot;>99.99</span></p>
<P><input type=checkbox name=&quot;2&quot; id=&quot;2&quot; onClick=&quot;updatePrice()&quot;>LCD Screen - $<span id=&quot;pr2&quot;>299.99</span></p>
<P><input type=checkbox name=&quot;3&quot; id=&quot;3&quot; onClick=&quot;updatePrice()&quot;>DVD ROM - $<span id=&quot;pr3&quot;>69.99</span></p>
<P><input type=checkbox name=&quot;4&quot; id=&quot;4&quot; onClick=&quot;updatePrice()&quot;>Cool Case - $<span id=&quot;pr4&quot;>109.99</span></p>

<P>Total Price <input name=&quot;ttl&quot; id=&quot;ttl&quot; readonly></p>
</form>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
 
Hi Pete,

I do a similar thing at

It's all JavaScript doing the calculations.

Feel free to lift what code you need. It calculates totals, as well as shipping (a simple formula) based on quantities of items.

Seeing as how you can't put anything on the server side (that would include CGI, of course), be extra-extra scrupulous when you receive your orders. There's always the &quot;JavaScript Programmer's Discount&quot;.

Cheers,


[monkey] Edward [monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top