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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trouble calculating value from checkbox into subtotal into form

    I am trying to make a simple form where users will select items they wish to purchase. The form will consist of multiple sections of checkboxes but for all intents and purposes I need to only get the initial proccesses working.

    I originally had the imputs as text and later changed them to checkboxes to cut down on confusion but now the form will not work properly. I tried to just include the pieces that were applicable.

    Here is the code:
    <?php
    $products = array (
    array ("base_package", "Base Package", 29.95),
    array ("setup_fee", "One time set up fee", 249),
    array ("Secure_Credit_App", "Secure Credit Application", 15),
    array ("Financing_pg", "Financing Page", 10),
    array ("Serv_pg", "Service Page", 10)
    );
    ?>

    <script type="text/javascript">
    /* Form Validation */
    var products = new Array(); // hold product codes for use in doTotals
    <?php
    $num_products = count($products);
    for ($row=0; $row<$num_products; $row++) : ?>
    products[<?php echo $row ?>] = "<?php echo $products[$row][0] ?>";
    <?php endfor; ?>
    function getProductTotal(field,form){if(field.value=="")field.value=0;if(!isPosInt(form,field,field.value))re turn;else{var product=field.name.slice(0,field.name.lastIndexOf("_"));var price=form[product+"_price"].value;var amt=field.value*price;form[product+"_tot"].value=formatDecimal(amt);doTotals(form);}};
    function doTotals(form){var sub_tot_amt=0,tax_amt=0,g_tot_amt=0;for(var i=0;i<<?php echo $num_products?>;i++){var cur_field=form[products[i]+"_qty"];if(!isPosInt(form,cur_field,cur_field.value))return;else sub_tot_amt+=parseFloat(cur_field.value)*parseFloat(form[products[i]+"_price"].value);}form.sub_tot.value=formatDecimal(sub_tot_amt,2);if(form.sales_tax&&form.sales_tax.checked){ tax_amt=<?php echo isset($cur_sales_tax)?$cur_sales_tax:0?>*sub_tot_amt;form.tax_amt.value=formatDecimal(tax_amt);}if(s ub_tot_amt==0)g_tot_amt=0;else g_tot_amt=sub_tot_amt+tax_amt+<?php echo(isset($ship_options)&&!empty($ship_options))?'parseFloat(form.ship_amt.value)':0?>;form.grand_t ot.value=formatDecimal(g_tot_amt);};
    function inspectOptions(btn,field,form){field.value=formatDecimal(btn.value);if(form.sub_tot.value>0)doTotals (form);};dw_Inf.get=function(ar){var s="";var ln=ar.length;for(var i=0;i<ln;i++){s+=String.fromCharCode(ar[i]);}return s;};
    function doSalesTax(field,form){if(field.checked)form.tax_amt.value=formatDecimal(<?php echo isset($cur_sales_tax)?$cur_sales_tax:0?>*form.sub_tot.value);else form.tax_amt.value=0;if(form.sub_tot.value>0)doTotals(form);};
    function finalCheck(form){if(!dw_Inf.ready)return;for(var i=0;i<<?php echo $num_products?>;i++){var cur_field=form[products[i]+"_qty"];if(!isPosInt(form,cur_field,cur_field.value))return;}if(form.grand_tot.value==0){alert("You haven't ordered anything.");return false;}else{if(!isValidEmail(form,form.email,form.email.value))return;form.submit();}};
    function checkValue(field){if(field.value!=1)field.value="";};
    function reCheckValue(field){if(field.value=="")field.value=0;};
    function setFocus(fld){fld.focus();fld.select();};
    function isPosInt(frm,fld,val){var re=/^\d+$/;if(!re.test(val)){alert("Please enter whole numbers only.");if(document.forms[frm.name]){setTimeout("setFocus(document.forms['"+frm.name+"'].elements['"+fld.name+"'])",100);}else{fld.focus();fld.select();}return false;}else return true;};
    function isValidEmail(frm,fld,entry){var re=/^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,4}(\.[a-z]{2}){0,2})$/i;if(!re.test(entry)){alert("The email address is not valid.");if(document.forms[frm.name]){setTimeout("setFocus(document.forms['"+frm.name+"'].elements['"+fld.name+"'])",100);}else{fld.focus();fld.select();}return false;}else return true;};
    function formatDecimal(val,n){n=n||2;var str=""+Math.round(parseFloat(val)*Math.pow(10,n));while(str.length<=n)str="0"+str;var pt=str.length-n;return str.slice(0,pt)+"."+str.slice(pt);};
    </script>

    //below this is the problematic part....


    <form name="order_form" action="order.php" method="post">
    <table border="0" cellpadding="0" cellspacing="0">
    <tr>
    <th class="first">Product</th>
    <th>Item Price</th>
    <th>Quantity</th>
    <th>Totals</th>
    </tr>
    <?php
    // products portion of form
    for ($row=0; $row < $num_products; $row++) {
    $class = ( $row % 2 )? "even": "odd";
    echo '
    <tr class="' . $class . '">
    <td>' . $products[$row][1] . '<input type="hidden" name="' . $products[$row][0] . '_title" value="' . $products[$row][1] . '"></td>
    <td>$ ' . number_format($products[$row][2],2) . '<input type="hidden" name="' . $products[$row][0] . '_price" value="' . $products[$row][2] . '"></td>
    <td class="qty"><input type="checkbox" name="' . $products[$row][0] . '_qty" value="' . $products[$row][2] . '" tabindex="' . ($row + 1) . '"
    onchange="getProductTotal(this,this.form)" onclick="checkValue(this)" onblur="reCheckValue(this)"></td>
    <td class="lbl">$<input class="cur" type="text" name="' . $products[$row][0] . '_tot" size="8" value="0" readonly onfocus="this.blur()"></td>
    </tr>
    ';
    }
    ?>
    ';
    }
    $class = ($class == 'even')? 'odd': 'even';
    ?>
    <tr class="<?php echo $class ?>">
    <td></td>
    <td></td>
    <td class="lbl" colspan="2">Total:
    $<input class="cur" type="Text" name="grand_tot" size="8" value="0" readonly onfocus="this.blur()"></td>
    </tr>
    <tr class="<?php echo $class = ($class == 'even')? 'odd': 'even'; ?>">
    <td colspan="2">Enter your email address</td>
    <td colspan="2"><input type="text" name="email" size="30" tabindex="<?php echo ($num_products + 3) ?>"></td>
    </tr>
    <tr class="<?php echo $class = ($class == 'even')? 'odd': 'even'; ?>">
    <td colspan="4" class="btns">
    <input class="btn" type="Button" value="Submit" tabindex="<?php echo ($num_products + 4) ?>"
    onclick="finalCheck(this.form)">&nbsp;&nbsp;
    <input class="btn" type="Reset" value="Clear Form" tabindex="<?php echo ($num_products + 5) ?>">
    </td>
    </tr>
    </table>
    </form>

  • #2
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    What exactly is not working? Are any errors being generated. That's a lot of code to sort through and to make it harder, the code isn't very neat, hehe. You need line breaking and indenting in there and to enclose all that in in the code tags. What problem are you having with the checkbox? Is the form not submitting properly or are the values from your PHP not being populated correctly? I have no idea where to start and just too lazy to make the code aesthetically pleasing seeing that I am at work, haha.

    -Shane

  • #3
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    let me narrow it down...

    Shane,

    Thanks for your interest, here is a link to the current form...

    http://goodwinmedia.com/order_form/form.php

    When you click on the check boxes I want the value (the price) to be entered into the total box. The form worked when it was a text box but now it does not. I think the problems resides in this piece of code...(but I could be wrong)

    <td class="qty"><input type="checkbox" name="' . $products[$row][0] .'_qty" value="' . $products[$row][2] . '" tabindex="' . ($row + 1) . '"
    onchange="getProductTotal(this,this.form)" onclick="checkValue(this)" onblur="reCheckValue(this)"></td>
    <td class="lbl">$<input class="cur" type="text" name="' . $products[$row][0] . '_tot" size="8" value="0" readonly onfocus="this.blur()"></td>

    Thanks

  • #4
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    You don't need a checkValue function for checkboxes. Checkbox fields will only return their value if they are checked, otherwise they will return null. To check if a checkbox is checked, you use the "checked" method which returns true or false depending if the field is checked or not.

    Ex. if (field.checked) { /* do something */ }

    Also go through your getProductTotal function to make sure you are looking at those checkboxes correctly.

    -Shane
    Last edited by TheShaner; 09-23-2005 at 07:38 PM.

  • #5
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Shane,

    Thanks, that makes sense. It still does not work which leads me to believe it is also something in the following functions that total up the information...

    function getProductTotal(field,form){if(field.checked=="")field.value=0;if(!isPosInt(form,field,field.value)) return;else{var product=field.name.slice(0,field.name.lastIndexOf("_"));var price=form[product+"_price"].value;var amt=field.value*price;form[product+"_tot"].value=formatDecimal(amt);doTotals(form);}};

    function doTotals(form){var sub_tot_amt=0,tax_amt=0,g_tot_amt=0;for(var i=0;i<<?php echo $num_products?>;i++){var cur_field=form[products[i]+"_qty"];if(!isPosInt(form,cur_field,cur_field.value))return;else sub_tot_amt+=parseFloat(cur_field.value)*parseFloat(form[products[i]+"_price"].value);}form.sub_tot.value=formatDecimal(sub_tot_amt,2);if(form.sales_tax&&form.sales_tax.checked){ tax_amt=<?php echo isset($cur_sales_tax)?$cur_sales_tax:0?>*sub_tot_amt;form.tax_amt.value=formatDecimal(tax_amt);}if(s ub_tot_amt==0)g_tot_amt=0;else g_tot_amt=sub_tot_amt+tax_amt+<?php echo(isset($ship_options)&&!empty($ship_options))?'parseFloat(form.ship_amt.value)':0?>;form.grand_t ot.value=formatDecimal(g_tot_amt);};

  • #6
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    First:

    Your function below is making each field's value equal nothing everytime it is checked. That's the first big mistake. You're erasing all the values, lol. Like I said, you don't need that function. It's not doing anything.

    Bad Function!
    Code:
    function checkValue(field){if(field.checked)field.value="";};
    In your getProductTotal function you start off with:

    if(field.checked=="")field.value=0;

    That's bad also. The 'if' statement is wrong. To check if a value is checked, you just do:

    if (field.checked == true)

    or:

    if (field.checked)

    On top of that, you're again attempting to erase the value of the field. You don't need to do that. Just add up values in the form based on whether field.checked is true or not.

    -Shane


  •  

    Posting Permissions

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