Automatic calculations

This is a discussion on "Automatic calculations" within the JavaScript Forum section. This forum, and the thread "Automatic calculations are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Dec 22nd, 2006, 14:28
Junior Member
Join Date: Dec 2006
Location: London
Age: 20
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Automatic calculations

Hi,

I'm working with a form at the moment in which someone requests a product, and it autmatically give a calculation when they click an option.

Thorough explanation:
This is a DVD creation business, for personal occasions, business conferenece slideshows, school presentations etc.
For each picture they wish to use will cost them, lets say, 15p, they access the dropdown menu and tick that they want, say 30 pictures, once they select this, the calculation will automatically update and show the total price.
(15p*30p = £4.50 obviously)

However, because i am new to javascript i have ran into a problem.
I have added a code in which this happens but dont know how to add another value without resulting in te first messing up.

HTML:

HTML: Select all
<strong><u>Pictures</u></strong>
<select onchange="formCalc();" id="picquantity">
    <option value="0">None</option>
    <option value="1">1 Pictures</option>
    <option value="2">2 Pictures</option>
    <option value="3">3 Pictures</option>
    <option value="4">4 Pictures</option>
    <option value="5">5 Pictures</option>
    <option value="6">6 Pictures</option>
    <option value="7">7 Pictures</option>
    <option value="8">8 Pictures</option>
    <option value="9">9 Pictures</option>
    <option value="10">10 Pictures</option>
    <option value="11">11 Pictures</option>
    <option value="12">12 Pictures</option>
    <option value="13">13 Pictures</option>
    <option value="14">14 Pictures</option>
    <option value="15">15 Pictures</option>
    <option value="16">16 Pictures</option>
    <option value="17">17 Pictures</option>
    <option value="18">18 Pictures</option>
    <option value="19">19 Pictures</option>
    <option value="20">20 Pictures</option>
    <option value="21">21 Pictures</option>
    <option value="22">22 Pictures</option>
    <option value="23">23 Pictures</option>
    <option value="24">24 Pictures</option>
    <option value="25">25 Pictures</option>
    <option value="26">26 Pictures</option>
    <option value="27">27 Pictures</option>
    <option value="28">28 Pictures</option>
    <option value="29">29 Pictures</option>
    <option value="30">30 Pictures</option>
    <option value="31">31 Pictures</option>
    <option value="32">32 Pictures</option>
    <option value="33">33 Pictures</option>
    <option value="34">34 Pictures</option>
    <option value="35">35 Pictures</option>
    <option value="36">36 Pictures</option>
    <option value="37">37 Pictures</option>
    <option value="38">38 Pictures</option>
    <option value="39">39 Pictures</option>
    <option value="40">40 Pictures</option>
    <option value="41">41 Pictures</option>
    <option value="42">42 Pictures</option>
    <option value="43">43 Pictures</option>
    <option value="44">44 Pictures</option>
    <option value="45">45 Pictures</option>
    <option value="46">46 Pictures</option>
    <option value="47">47 Pictures</option>
    <option value="48">48 Pictures</option>
    <option value="49">49 Pictures</option>
    <option value="50">50 Pictures</option>
    <option value="51">51 Pictures</option>
    <option value="52">52 Pictures</option>
    <option value="53">53 Pictures</option>
    <option value="54">54 Pictures</option>
    <option value="55">55 Pictures</option>
    <option value="56">56 Pictures</option>
    <option value="57">57 Pictures</option>
    <option value="58">58 Pictures</option>
    <option value="59">59 Pictures</option>
    <option value="60">60 Pictures</option>
    <option value="61">61 Pictures</option>
    <option value="62">62 Pictures</option>
    <option value="63">63 Pictures</option>
    <option value="64">64 Pictures</option>
    <option value="65">65 Pictures</option>
    <option value="66">66 Pictures</option>
    <option value="67">67 Pictures</option>
    <option value="68">68 Pictures</option>
    <option value="69">69 Pictures</option>
    <option value="70">70 Pictures</option>
    <option value="71">71 Pictures</option>
    <option value="72">72 Pictures</option>
    <option value="73">73 Pictures</option>
    <option value="74">74 Pictures</option>
    <option value="75">75 Pictures</option>
    <option value="76">76 Pictures</option>
    <option value="77">77 Pictures</option>
    <option value="78">78 Pictures</option>
    <option value="79">79 Pictures</option>
    <option value="80">80 Pictures</option>
    <option value="81">81 Pictures</option>
    <option value="82">82 Pictures</option>
    <option value="83">83 Pictures</option>
    <option value="84">84 Pictures</option>
    <option value="85">85 Pictures</option>
    <option value="86">86 Pictures</option>
    <option value="87">87 Pictures</option>
    <option value="88">88 Pictures</option>
    <option value="89">89 Pictures</option>
    <option value="90">90 Pictures</option>
    <option value="91">91 Pictures</option>
    <option value="92">92 Pictures</option>
    <option value="93">93 Pictures</option>
    <option value="94">94 Pictures</option>
    <option value="95">95 Pictures</option>
    <option value="96">96 Pictures</option>
    <option value="97">97 Pictures</option>
    <option value="98">98 Pictures</option>
    <option value="99">99 Pictures</option>
    <option value="100">100 Pictures</option>
 
</select>
 
 
<input type="hidden" id="picprice" value="0.15" onBlur="formCalc();" />
 
<input type="text" id="total" value="" readonly />
javascript:

Code: Select all
 
<script type="text/javascript">
 
function formCalc() {
    var picquantity = document.getElementById('picquantity').value;
    var picprice = document.getElementById('picprice').value;
    document.getElementById('total').value = picquantity * picprice;
}
 
</script>
thanks in advance
regard,
joe.
Reply With Quote

  #2 (permalink)  
Old Dec 22nd, 2006, 15:05
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: Automatic calculations

Don't make your price field hidden. Just make it read only so the user cannot alter it and get rid of the onblur() function.
Quote:
I have added a code in which this happens but dont know how to add another value without resulting in te first messing up.
Not quite sure what you are getting at here.

Why not let the user just type in a quanity rather than have a huge long selection?
Reply With Quote
  #3 (permalink)  
Old Dec 22nd, 2006, 15:10
Junior Member
Join Date: Dec 2006
Location: London
Age: 20
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Automatic calculations

Quote:
Originally Posted by ukgeoff View Post

Why not let the user just type in a quanity rather than have a huge long selection?
Because there will be a limit on how many they can have, we have limited to only 200 photos per request.

Unless you think there is an easier way?

Thanks.
Reply With Quote
  #4 (permalink)  
Old Dec 22nd, 2006, 15:23
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: Automatic calculations

You didn't answer my query about 'messing up', or has the first bit of my response fixed everything?
Reply With Quote
  #5 (permalink)  
Old Dec 22nd, 2006, 15:52
Junior Member
Join Date: Dec 2006
Location: London
Age: 20
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Automatic calculations

Oh, apologies.

What i mean by 'messing up' is:

Although the code provided does work in terms of calculating the value, when i try to add another dropdown box, i believed that adding the simple code to both:

javascript:

Code: Select all
function formCalc() {
    var musicquantity = document.getElementById('musicquantity').value;
    var musicprice = document.getElementById('musicprice').value;
    document.getElementById('total').value = musicquantity * musicprice;
}
HTML:

HTML: Select all
<strong><u>Pictures</u></strong>
<select onchange="formCalc();" id="musicquantity">
<option value="0">None</option>
<option value="1">1 Song</option>
<option value="2">2 Songs</option>
<option value="3">3 Songs</option>
 
</select>
 
<input type="hidden" id="musicprice" value="3.95" onBlur="formCalc();" />
The end value ends up either Nan, or doesnt show up at all.

Last edited by josephman1988; Dec 22nd, 2006 at 15:57.
Reply With Quote
  #6 (permalink)  
Old Dec 22nd, 2006, 16:12
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: Automatic calculations

Include a parameter in your function call so that the function can determine which dropdown initiated it and act accordingly.
Reply With Quote
  #7 (permalink)  
Old Dec 22nd, 2006, 17:25
Junior Member
Join Date: Dec 2006
Location: London
Age: 20
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Automatic calculations

How would I do this, sorry for being a troublesome newbie but we have to start somewhere. Thanks again.
Reply With Quote
  #8 (permalink)  
Old Dec 22nd, 2006, 17:44
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,620
Blog Entries: 1
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: Automatic calculations

function sample(parameter){ // taking in the parameter
var a = parameter + 9;
}


sample(1); // sending the parameter
// the example above will store the value 10 in variable 'a'
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Reply With Quote
Reply

Tags
auto, calculations

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic Scheduling Jackus PHP Forum 4 Apr 5th, 2008 01:59
Automatic Backups Gee Bee Databases 9 Jan 22nd, 2006 02:48
Automatic Sending accessman Databases 0 Sep 25th, 2005 17:25
Automatic Automated mail out briandmenupalace Classic ASP 4 Jul 14th, 2005 17:58
automatic redirects benbacardi Classic ASP 2 Sep 13th, 2004 09:02


All times are GMT. The time now is 04:19.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43