Move Javascript Computation to PHP mail script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • microtribe
    New Member
    • Nov 2007
    • 4

    Move Javascript Computation to PHP mail script

    Hi,

    I have a Javascript total calculation function within a php form that uses onBlur to show the client a running total of the dollar amount of items added: http://www.microtribe.com/dev4Tony/order881x2.php

    And I have a linked external PHP file/script to email the results of the form to me. All works fine but I can't figure out how to grab the total dollar amount ("sub_total" ) created by the Javascript with this line:
    document.getEle mentById('sub_t otal').innerHTM L = '$ ' + runningTotal.to Fixed(2);

    Is there a way to have the Javascript send this variable to my php script?

    Here is the full Javascript Function:

    [CODE=javascript]/*Addition Function
    */
    var elements = new Array();

    function calculatePrice( price,me,id) {
    var newValue = 0;
    if (me.value == 0) {
    document.getEle mentById('total ' + id).innerHTML = '';
    } else if (me.value > 0) {
    newValue = price * me.value;
    document.getEle mentById('total ' + id).innerHTML = newValue.toFixe d(2);
    }

    elements['total' + id] = newValue.toFixe d(2);

    calculateTotal( );

    }

    function calculateTotal( ) {
    var runningTotal = 0;
    for (var strCurrentKey in elements) {
    runningTotal += parseFloat(elem ents[strCurrentKey]);
    }

    var sub = runningTotal.to Fixed(2);
    var tax = sub * 0.00;
    var total = sub * 1.08375;

    document.getEle mentById('sub_t otal').innerHTM L = '$ ' + runningTotal.to Fixed(2);
    document.getEle mentById('tax') .innerHTML = '$ ' + tax.toFixed(2);
    document.getEle mentById('grand _total').innerH TML = '$ ' + total.toFixed(2 );

    document.getEle mentById('field _subtotal').val ue = runningTotal.to Fixed(2);
    document.getEle mentById('field _tax').value = tax.toFixed(2);
    document.getEle mentById('field _total').value = total.toFixed(2 );

    }


    /*Parse number to currency format:
    By Website Abstraction (www.wsabstract .com)
    and Java-scripts.net (www.java-scripts.net)
    */

    //Remove the $ sign if you wish the parse number to NOT include it
    var wd;
    function parseelement(te mpnum){
    wd="w";
    for (i=0;i<tempnum. length;i++){
    if (tempnum.charAt (i)=="."){
    wd="d";
    break;
    }
    }
    if (wd=="w") {
    return tempnum+".00";
    } else {
    if (tempnum.charAt (tempnum.length-2)==".") {
    return tempnum+"0";
    } else {
    tempnum=Math.ro und(tempnum*100 )/100;
    return tempnum;
    }
    }
    }
    [/CODE]
    Thanks!
    Tony
    Last edited by gits; Nov 24 '07, 07:11 PM. Reason: added code tags
  • microtribe
    New Member
    • Nov 2007
    • 4

    #2
    So it won't work to move the Javascript total into a hidden field - as below (which I tried):

    In js script:
    [CODE=javascript]document.getEle mentById('subto tal').value = '$ ' + runningTotal.to Fixed(2);[/CODE]
    On HTML form:
    [HTML]<input type="hidden" id="subtotal" name="subtotal" value="">
    [/HTML]In php script:
    [PHP]$sub_total = $_POST['subtotal'];[/PHP]

    I am guessing it isn't possible, although I don't understand why I can get that total into a div tag, and not into a hidden field that I can then $_POST to my php form.

    This part works:

    js script:
    [CODE=javascript]document.getEle mentById('sub_t otal').innerHTM L = '$ ' + runningTotal.to Fixed(2);
    [/CODE]
    HTML form:
    [HTML]<div class="price" id="sub_total" name="sub_total "> $0.00</div>[/HTML]

    And using onBlur to call the js function, the total is updated everytime someone tabs out of a qty field.

    So why can that total make it to the div tag and not the hidden field? If I could just get it to the hidden field, I could pass it to the php script...right?

    Also - I'm confused by the naming of the js code getElementById. It seems like this code is "putting" my subtotal (the runningTotal.to Fixed(2) piece?) into the div tag - correct? So I don't understand why it is call "getElement ". I'm assuming that I don't really understand what's happening. Can someone explain?

    Thanks for your help.

    P.S. At this point I've spent a million hours on this project, just trying to figure out this last piece, so if someone has a better suggestion for how to have implemented this, I'd appreciate learning to do it the "right" way...

    I have an order form that needs to calculate a running total of items ordered, and show the totals as they go (all on one page) and then send all the info by email once the order is complete. (no payment function needed).
    Last edited by acoder; Nov 26 '07, 11:35 AM. Reason: Added code tags

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Where or when do you set the hidden field?

      Do you get any errors?

      Comment

      • microtribe
        New Member
        • Nov 2007
        • 4

        #4
        no errors. when the form is submitted i get all of the values EXCEPT the total.

        the .js function calculates the total and it appears on the users web page in the div tag quoted above. it will not put that same value in a hidden field though, which is then passed to my php script that mails me the value.

        Originally posted by acoder
        Where or when do you set the hidden field?

        Do you get any errors?

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          You have errors: document.getEle mentById("tax") has no properties. This is probably preventing the hidden field from being set.

          Comment

          Working...