Updating and recalculating the checkout page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rostok
Forum Newbie
Posts: 3
Joined: Fri Feb 09, 2007 2:12 pm

Updating and recalculating the checkout page

Post by Rostok »

Hopefully I will manage to explain my problem and you will have a solution for it :).
I have a checkout page, coded in PHP (by some other coder, not available anymore). On the page I have one table containing shopping cart items and one submission form, to collect all information from the customer. When the page is loaded, the table is filled with data from the temporary data table through SQL query (collected on the previous page, shopping cart page), except for the field about the amount of the sales tax, which is not yet known because I don’t know anything about the customer. So I have a shopping cart table and a blank form below that, ready to be filled with customer’s information.

Now, my question is: How can I calculate and fill the sales tax field in the shopping cart table, if the customer chooses his state from the drop down list in the form? I must say here that the sales tax is only applicable for residents of just one state, Vermont, because the company is based in Vermont. For instance, if the customer chooses Vermont as his state, how this can be translated into sales tax amount, added into the field in the shopping cart table and recalculated the total amount? Should the page be reloaded, with changed value of some variable for the sales tax? Should the JavaScript be used?
All information entered in the form should be kept after the selection of the state and calculation is made (I assume that the page should be reloaded/refreshed), so the user shouldn’t have to re-enter them again.
After this recalculation, the customer hits the “Make Purchase” button and the form is submitted and the receipt for the purchase is sent.

I’m not very familiar with PHP or JavaScript, so any help is greatly appreciated, maybe with some code examples.


Thanks,
Robert
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

Code: Select all

$vermont_tax = "12%";//Vermont tax standard
$customer_location = $row['location']; // customer location from database

if(strtolower($customer_location) == "vermont"){ 
  $cart_total = ($cart_total *1.(str_replace("%","",$vermont_tax))); //we do the math
}
hope it gives you an idea.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Don't rely on Javascript to do this for you. Work it on the server.

What I would do is, when the user selected their state, have the form post back to itself. You can keep all of the posted information from the form in the $_POST array and fill teh form back in for the user with the data they entered. At this point, I would check the state, and if the state happens to be Vermont, I would take the total and multiply that by 0.12 to get the tax, then add that value to the total to get the new total. I would also be nice and post a little warning to the user saying that Vermont requires the collection of Sales Tax for residents of Vermont or something to that effect.

Code: Select all

<?php
$total = 350; // So value from the past operation
$tax_due = 0.00; // Notice the decimal point...

$state_selected = '';

// Handle the form submission for the state selection, and if it is good, process
...
// Now form handling is done, do some math if the state is Vermont.
if ($state_selected == 'VT')
{
  $tax_due = 0.12 * $total; // Again, use a decimal point
}

$total_due = $total + $tax_due;
$total_due = number_format($total_due, 2, '.', ',');
?>
Useful links for handling floating point numbers can be found here and here.
Post Reply