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

Thread: Pass Javascript variable to hidden field and php form

Threaded View

  1. #1
    Join Date
    Nov 2007

    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:

    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:

    /*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);
    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";
    Last edited by PeejAvery; December 2nd, 2007 at 01:23 PM. Reason: Added code tags.

Posting Permissions

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

Azure Activities Information Page

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


HTML5 Development Center