javascript - Getting tax price to calculate before submitting a form -
i'm trying tax value in checkout page calculate once state field selected. want without having submit form want display tax rate on page before order processed.
i sure have ajax , have used ajax limited amount of times. know send request ajax php page , ajax pulls field need. need pull is, if ohio selected need pull value of 1.065. if other state pulled need value of 1.
i have state variables listed in array format because using options state dropdown..
$taxvalue['ohio'] = "ohio"; $taxvalue['virginia'] = "virginia";
etc
then tried if isset statement when ohio selected pull $taxed_state value of 1.065. else else untaxed_state.
$taxed_state = 1.065; $untaxed_state = 1; if(isset($taxvalue['ohio'])){ $taxed_state; } else { $untaxed_state; }
this how trying configure tax rate know work once can state set before form submitted.
$base_price = 0; foreach($_session['shopping_cart'] $id => $product) { $product_id = $product['product_id']; $base_price += $products[$product_id]['price'] * $product['quantity']; $shipping_price += $products[$product_id]['shippingprice'] * $product['quantity']; } $tax_price += $base_price * $taxed_state; $total_price += $base_price + $shipping_price + $tax_price; }
i know general way send ajax call...
$.ajax({ url: "tax.php", data: { action: "" }, type: "post", datatype: "text" }).fail(function(e, t, m) {
but i'm not sure how structure ajax call make work trying do. how can go doing getting tax rate sent 1 state input field selected.
i'm sure i'm pretty far off on this, give insight , point me in right direction.
update:
i'm putting in more code see i'm doing wrong..
$taxvalue['wisconsin'] = 1; $taxvalue['wyoming'] = 1; $taxed_state = 1.065; $untaxed_state = 1; if(isset($taxvalue['ohio'])){ $taxed_state; } else { $untaxed_state; } $base_price = 0; foreach($_session['shopping_cart'] $id => $product) { $product_id = $product['product_id']; $base_price += $products[$product_id]['price'] * $product['quantity']; $shipping_price += $products[$product_id]['shippingprice'] * $product['quantity']; } $tax_price += $base_price * $taxed_state; $total_price += $base_price + $shipping_price + $tax_price; } ?> <div id="msgdiv" > </div> <div class="ordersummarycontainer"> <span class="summarytitle"><p>order summary</p></span><br> items: <span class="floatright"><?php echo "$" . $base_price; ?></span> <p>shipping , handling: <span class="floatright"><?php echo "$" . $shipping_price; ?></span></p> <p>tax: <span class="floatright"><?php echo "$" . $tax_price - $base_price; ?></span></p>
at bottom of page have js
<script> $(document).ready(function() { $(function() { var taxed_price = 0; $( "#taxvalue" ).change(function() { if ($(this).val() == 'ohio') { tax = 1.065; } else { tax = 1; } $('#taxed_price').val(base_total * taxed_state); }); }); }); </script>
update:>
i commented out $taxed_state , untaxed_state because had values in it, trying were.
$taxvalue['wisconsin'] = 1; $taxvalue['wyoming'] = 1; //$taxed_state = 1.065; //$untaxed_state = 1; if(isset($taxvalue['ohio'])){ $taxed_state; } else { $untaxed_state; } $base_price = 0; foreach($_session['shopping_cart'] $id => $product) { $product_id = $product['product_id']; $base_price += $products[$product_id]['price'] * $product['quantity']; $shipping_price += $products[$product_id]['shippingprice'] * $product['quantity']; } $tax_price += $base_price * $taxed_state; $total_price += $base_price + $shipping_price + $tax_price; }
the area tax echod to. area not getting show up
<p>tax: <span class="floatright"><?php echo "$" . $tax_price //- $base_price; ?></span></p>
form state drop down is...
<label for="state">state</label> <select type="text" class="mediuminputbar preview" id="shiptostate taxvalue" data-copy="#confirmstate" name="shiptostate taxvalue" value="<?php echo escape($user->data()->state); ?>" required> <option value=''>select state</option> <?php foreach($taxvalue $key => $val) { echo "<option value='$val'>$key</option>/n"; } ?> </select>
js
$(document).ready(function() { $(function() { var taxed_state = 0; var base_total = 0; $( "#taxvalue" ).change(function() { if ($(this).val() == 'ohio') { taxed_state = 1.065; } else { taxed_state = 1; } $('#taxed_price').val(base_total * taxed_state); }); }); });
it seems me might not need ajax call. if have 2 tax rates (ohio , non-taxed), why not through javascript? this:
$(function() { var tax = 0; $( "#state" ).change(function() { if ($(this).val() == 'ohio') { tax = 1.065; } else { tax = 1; } $('#total').val(total * tax); }); });
Comments
Post a Comment