Real Time Tax rates


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?
  2. // the 2 post items that we need for tax.
  3. $zip = $_POST['Ship-Zip'];
  4. $total = $_POST['Taxable-Total'];
  5. // connect to your mysql db, with yahoo your localhost is mysql
  6. mysql_connect("mysql", "username", "password") or die(mysql_error());
  7. mysql_select_db("tax_rates") or die(mysql_error());
  8. $result = mysql_query("SELECT * FROM `zip_codes` WHERE `zipcode` LIKE CONVERT( _utf8 '$zip' USING latin1 )")
  9. $num_rows = mysql_num_rows($result);
  10. // this checks if a zip code is returned, if no then no tax is charged
  11. if($num_rows != "0"){
  12. while($row = mysql_fetch_array( $result )) {
  13. $percent = $row['taxrate'];
  14. }
  15. $to = ($percent/100)*$total;
  16. }else {
  17. $to = "0";
  18. }
  19. //needs the 200 ok header for the cart to see it.
  20. header("HTTP/1.0 200 OK");
  21. header("Tax-Charge: $to");
  22. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.