Pass Javascript variable to hidden field and php form
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Pass Javascript variable to hidden field and php form

  1. #1
    Join Date
    Nov 2007
    Posts
    2

    Pass Javascript variable to hidden field and php form

    I have a Javascript total calculation function in an external .js file called from a php form using 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 an 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") that is calculated by the Javascript and returned to a div tag on my form with this line:
    document.getElementById('sub_total').innerHTML = '$ ' + runningTotal.toFixed(2);

    Is there a way to have the Javascript send this variable to a hidden field on my form which I can then pass to my php script to email? I am able to get the total into a div tag, but not a hidden field.

    Here is the full Javascript Function:

    Code:
    /*Addition Function
    */
    var elements = new Array();
    
    function calculatePrice(price,me,id) {
    var newValue = 0;
    if (me.value == 0) {
    document.getElementById('total' + id).innerHTML = '';
    } else if (me.value > 0) {
    newValue = price * me.value;
    document.getElementById('total' + id).innerHTML = newValue.toFixed(2);
    }
    
    elements['total' + id] = newValue.toFixed(2);
    
    calculateTotal();
    
    }
    
    function calculateTotal() {
    var runningTotal = 0;
    for (var strCurrentKey in elements) {
    runningTotal += parseFloat(elements[strCurrentKey]);
    }
    
    var sub = runningTotal.toFixed(2);
    var tax = sub * 0.00;
    var total = sub * 1.08375;
    
    document.getElementById('sub_total').innerHTML = '$ ' + runningTotal.toFixed(2);
    	document.getElementById('tax').innerHTML = '$ ' + tax.toFixed(2);
    	document.getElementById('grand_total').innerHTML = '$ ' + total.toFixed(2);
    
    	document.order_guide.subtotal.value = '$ '+runningTotal.toFixed(2);
    	document.getElementById('field_subtotal').value = runningTotal.toFixed(2);
    	document.getElementById('field_tax').value = tax.toFixed(2);
    	document.getElementById('field_total').value = total.toFixed(2);
    
    }
    
    
    Here are relevant bits of the php form:
      <form enctype="multipart/form-data" name="order_guide" action="sendorder.php" method="post">
    <input name="subtotal" type="hidden" id="subtotal" value="">
    
    And relevant bits of my php script:
    	 $sub_total = $_POST['subtotal'];
    $msg .= "ORDER SUBTOTAL: $sub_total\n";
    Thanks!
    Tony
    Last edited by PeejAvery; December 2nd, 2007 at 02:23 PM. Reason: Added code tags.

  2. #2
    Join Date
    Nov 2005
    Posts
    49

    Re: Pass Javascript variable to hidden field and php form

    Have u try this:
    Code:
    document.getElementById("subtotal").value = total;
    Muhammad Waqas Badar

  3. #3
    Join Date
    Nov 2007
    Posts
    2

    Re: Pass Javascript variable to hidden field and php form

    Thanks. I tried that but it didn't work...Any other ideas?

  4. #4
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Pass Javascript variable to hidden field and php form

    why there is the enctype="multipart/form-data" attribute in the form? usually, we don't specify enctype for POST method. you may try to remove that as it is intended for file upload..

    Code:
    <form name="order_guide" action="sendorder.php" method="post">
    <input name="subtotal" type="hidden" id="subtotal" value="">
    Busy

  5. #5
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,881

    Re: Pass Javascript variable to hidden field and php form

    Waqas_Badar's suggestion is correct. Are you sure you are using it right? Also, you can alert the hidden input after you set it to make sure the data is set properly.

    Code:
    var objSubTotal = document.getElementById('subtotal');
    objSubTotal.value = total;
    alert(objSubTotal.value);
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  6. #6
    Join Date
    Sep 2012
    Posts
    1

    Re: Pass Javascript variable to hidden field and php form

    Thanks this helped a lot on my project!

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center