Web Analytics Made Easy -
StatCounter Java string into post data - CodingForum

Announcement

Collapse
No announcement yet.

Java string into post data

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Java string into post data

    Hi i have a javascript to do a basic calculation that i found in another code forum. I have adapted it to suit my needs but i want to send the calculation as post data on submit.

    its very simple

    Code:
    <[COLOR="RoyalBlue"]html[/COLOR]>
    <[COLOR="RoyalBlue"]head[/COLOR]>
    <[COLOR="RoyalBlue"]script[/COLOR]>
    function [COLOR="Blue"]calc[/COLOR]([COLOR="Blue"]theForm[/COLOR]) {
      var [COLOR="Blue"]$value1[/COLOR] = ([COLOR="Blue"]parseFloat[/COLOR]([COLOR="Blue"]theForm[/COLOR].[COLOR="Blue"]value1[/COLOR].[COLOR="Blue"]options[/COLOR][[COLOR="Blue"]theForm[/COLOR].[COLOR="Blue"]value1[/COLOR].[COLOR="Blue"]selectedIndex[/COLOR]].[COLOR="Blue"]value[/COLOR])||[COLOR="Green"]0[/COLOR])
      var [COLOR="Blue"]$value2[/COLOR] = ([COLOR="Blue"]parseFloat[/COLOR]([COLOR="Blue"]theForm[/COLOR].[COLOR="Blue"]value2[/COLOR].[COLOR="Blue"]options[/COLOR][[COLOR="Blue"]theForm[/COLOR].[COLOR="Blue"]value2[/COLOR].[COLOR="Blue"]selectedIndex[/COLOR]].[COLOR="Blue"]value[/COLOR])||[COLOR="Green"]0[/COLOR])
      var [COLOR="Blue"]$total[/COLOR]=[COLOR="Green"]0[/COLOR] + ([COLOR="Green"]250[/COLOR] * [COLOR="Blue"]$value1[/COLOR]) + ([COLOR="Green"]50[/COLOR] * [COLOR="Blue"]$value2[/COLOR]);
      [COLOR="Blue"]document[/COLOR].[COLOR="Blue"]getElementById[/COLOR]([COLOR="Red"]'showTotal'[/COLOR]).[COLOR="Blue"]innerHTML[/COLOR]=[COLOR="Red"]'£'[/COLOR]+[COLOR="Blue"]$total[/COLOR].[COLOR="Blue"]toFixed[/COLOR]([COLOR="Green"]2[/COLOR])
    }
    <[COLOR="RoyalBlue"]/script[/COLOR]>
    <[COLOR="RoyalBlue"]/head[/COLOR]>
    <[COLOR="RoyalBlue"]body[/COLOR] [COLOR="Green"]onLoad[/COLOR]="calc(document.forms[0])">
    <[COLOR="RoyalBlue"]form [/COLOR][COLOR="Green"]method[/COLOR]="post" [COLOR="Green"]action[/COLOR]="next.php">
    
    
    <[COLOR="RoyalBlue"]select [/COLOR][COLOR="Green"]name[/COLOR]="value1"  [COLOR="Green"]onChange[/COLOR]="calc(this.form)">
    <[COLOR="RoyalBlue"]option [/COLOR][COLOR="Green"]value[/COLOR]="1" selected="selected">1<[COLOR="RoyalBlue"]/option[/COLOR]>
    <[COLOR="RoyalBlue"]option[/COLOR] [COLOR="Green"]value[/COLOR]="2">2<[COLOR="RoyalBlue"]/option[/COLOR]>
    </[COLOR="RoyalBlue"]select[/COLOR]>Number of seats<[COLOR="RoyalBlue"]br[/COLOR]><[COLOR="RoyalBlue"]br[/COLOR]>
    
    <[COLOR="RoyalBlue"]select [/COLOR][COLOR="Green"]name[/COLOR]="value2"  [COLOR="Green"]onChange[/COLOR]="calc(this.form)">
    <[COLOR="RoyalBlue"]option [/COLOR][COLOR="Green"]value[/COLOR]="0" selected="selected">0<[COLOR="RoyalBlue"]/option[/COLOR]>
    <[COLOR="RoyalBlue"]option[/COLOR] [COLOR="Green"]value[/COLOR]="1">1<[COLOR="RoyalBlue"]/option[/COLOR]>
    </[COLOR="RoyalBlue"]select[/COLOR]>Number of rooms<[COLOR="RoyalBlue"]br[/COLOR]><[COLOR="RoyalBlue"]br[/COLOR]>
    
    <[COLOR="RoyalBlue"]input[/COLOR] [COLOR="Green"]type[/COLOR]="submit" [COLOR="Green"]name[/COLOR]="submit" [COLOR="Green"]value[/COLOR]="submit"><br>
    </[COLOR="RoyalBlue"]form[/COLOR]>
    Total: <[COLOR="RoyalBlue"]div[/COLOR] [COLOR="Green"]id[/COLOR]="showTotal">0.00</[COLOR="RoyalBlue"]div[/COLOR]>
    </[COLOR="RoyalBlue"]body[/COLOR]>
    </[COLOR="RoyalBlue"]html[/COLOR]>
    I have need to add a hidden field containing the calculation result

    <input type="hidden" name="total" value=" ??? ">

    I am new to javascript and i am also consulting some ebooks but if any one has a solution that would be grand

    Kind regards
    Ben

  • #2
    Code:
    document.getElementById('showTotal').innerHTML='£'+$total.toFixed(2);
    document.getElementById('total').value='£'+$total.toFixed(2);// or just the value without £ symbol, which you may add after submit.
    Code:
    <input type="hidden" name="total" id="total">
    Digitalocean Cloud Hosting (Referral link - get $10 free credit) Fameco

    Comment


    • #3
      Thanks that worked a treat and quite simple too, one day my boss will pay for some programming courses and give me the time off to do them.

      Thanks again for the quick reply
      All the best

      ben

      Comment

      Working...
      X