Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Web Form calculation not working

    Iv started to edit a php web form , and iv got some of the javascript code needed for it to work, However I am missing a vital piece which I cannot seem to work out.

    What should happen is this :

    When a user selects the advert required , and the number of issues they need from the drop down boxes , the sub total calculation should take place.

    The old form can be seen here under place an order (dont actually place the order because its a live form) [link removed]

    The number of issues isnt there because it was all included in one drop down box. Im trying to shorten this first box and make it easier to read.


    I dont know what the best way to do this is , so iv included a link to the page with the new form bellow aswell as what I have so far for the javascript code.

    link to new form : [link removed]

    Code:
    function formatPrice(theField){
    
      if(isNaN(theField.value)){
    
        theField.select();
    
        theField.focus();
    
        alert('Invalid price: '+theField.value);
    
        return false;
    
      }
    
      price = Math.round(100+theField.value*100)+'';
    
      theField.value = parseInt((price-100)/100)+'.'+price.substr(price.length-2);
    
    }
    
    function calculate() {
    
    adcharge = 0;
    
    designcharge = 0;
    
    thetotal = 0;
    
    vatrate = 0.20;
    
    cardfee = 0;
    
    grand_total = 0;
    
    issue =  1;
    
    var money = grand_total;
    
    if (document.getElementById('advert').value == "FullPage" {adcharge = 188;}
    if (document.getElementById('advert').value == "HalfPage" {adcharge = 200;}
    if (document.getElementById('advert').value == "QuarterPage" {adcharge = 245;}
    if (document.getElementById('advert').value == "EighthPage" {adcharge = 288;}
    
    document.getElementById('ad_charge').value = adcharge;
    
    formatPrice(document.getElementById('ad_charge'));
    
    switch(document.getElementById('issues').value) {
        case 2:
            adcharge *= 2;
            adcharge *= 0.9; 
            break;
        case 3:
            adcharge *= 3;
            adcharge *= 0.85;
            break;
        case 6:
            adcharge *= 6;
            adcharge *= 0.85;
            break;
     }
    
    
    if (document.getElementById('design_service').checked == true){designcharge = 20;}
    
    document.getElementById('design_charge').value = designcharge;
    
    formatPrice(document.getElementById('design_charge'));
    
    
    
    document.getElementById('sub_total').value = adcharge + designcharge;
    
    formatPrice(document.getElementById('sub_total'));
    
    
    
    thetotal = adcharge + designcharge;
    
    
    
    document.getElementById('vat').value = thetotal * vatrate;
    
    formatPrice(document.getElementById('vat'));
    
    
    
    document.getElementById('total').value = thetotal * (1 + vatrate);
    
    formatPrice(document.getElementById('total'));
    
    
    
    if (document.getElementById('payment_method').value == "PayPal") {cardfee = ((thetotal * (1 + vatrate)) * 0.024)+0.24;}
    
    document.getElementById('card_fee').value = cardfee;
    
    formatPrice(document.getElementById('card_fee'));
    
    
    
    document.getElementById('grand_total').value = (thetotal * (1 + vatrate)) + cardfee;
    
    formatPrice(document.getElementById('grand_total'));
    
    return true;
    
    }
    Last edited by VIPStephan; 10-30-2013 at 12:07 PM. Reason: removed link upon request by OP

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    I wanted to answer this question so I could say that the abbreviation of I have is I've, not iv. It rubbed me wrong.
    This answer is only for the three things you stated. If you want to add the other two check box values it should be easy, If not say so and I'll add them.
    This also uses universal variables. Something I only do if I can not figure out a different.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    </head>
    <body>
    <form enctype="multipart/form-data" action='#order' method="post" onsubmit="return validate()" id="orderform">
    	<label for="advert" class="formlabel">Advert</label>
    	<select name="advert" id="advert" onchange="calc1(this.value);">
    		<option value="">Please select the advert required</option>
    		<option value=188>Full Page Display Advert (136mm x 190mm)</option>
    		<option value=200>Half Page Display Advert (136mm x 93mm)</option>
    		<option value=245>Quarter Page Display Advert (66mm x 93mm)</option>
    		<option value=288>Eighth Page Display Advert (66mm x 44.5mm) minimum 2 issues</option>
    	</select>
    	<br />
    
    	<label for="issues" class="formlabel">Number of issues</label>
    	<select name="issues" id="issues" onchange="calc2(this.value);">
    		<option value="">Please select the number of issues required</option>
    		<option value=1>1</option>
    		<option value=2>2</option>
    		<option value=3>3</option>
    		<option value=6>6</option>
    	</select>
    	<br />
    
    	<div class="formfield"><label for="sub_total" class="formlabel">Sub-total</label>
    	<input type="text" name="sub_total" id="sub_total" value="0" size="7" readonly class="plaintextbox" />
    	</div>
    </form>
    
    <script type="text/javascript">
    cost_one = 0;
    cost_two = 0;
    function calc1(amt){
    	cost_one = amt;
    	tot();
    }
    function calc2(amt){
    	cost_two = amt;
    	tot();
    }
    function tot(){
    	cost = cost_one * cost_two;
    	if(cost > 0)
    		document.getElementById('sub_total').value = cost;
    }
    </script>
    </body>
    </html>
    Evolution - The non-random survival of random variants.

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,467
    Thanks
    3
    Thanked 495 Times in 482 Posts
    This also uses universal variables. Something I only do if I can not figure out a different.
    Code:
    function calc1 (amt){
        calc1.amt=amt;
    	tot();
    }
    function calc2(amt){
    	cost_two = amt;
    	tot();
    }
    
    function tot(){
    	cost = (calc1.amt||0) * (calc2.amt||0);
    	if(cost > 0)
    		document.getElementById('sub_total').value = cost;
    }
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •