I'm new to this forum but I do read posts as a non-member and I learn a lot from reading other posts from other member. I'm having problem understand how to write the code correctly. I can't seen to figure out the international shipping cost but for domestic it works. What can I write to make the international shipping cost work? I really appreciate if someone explain or show me how to write it so I can learn it and get a better understand how this language work.
Here is the code:
<?php
session_start();
header("Cache-control: private");
require_once ('include/init.php');
if (isset($_POST['ins'])) {
if (!isset($_POST['promo'])) {$promo="no";} else {$promo="yes";}
$sstat = $_POST["stat"];
$sql="INSERT INTO cust(fname, lname, email, addr, city, stat, zip, promo, phone, dt, country) VALUES (".
"'".mysql_real_escape_string(trim($_POST["fname"]))."', ".
"'".mysql_real_escape_string(trim($_POST["lname"]))."', ".
"'".mysql_real_escape_string(trim($_POST["email"]))."', ".
"'".mysql_real_escape_string(trim($_POST["addr"]))."', ".
"'".mysql_real_escape_string(trim($_POST["city"]))."', ".
"'".mysql_real_escape_string(trim($_POST["stat1"]))."', ".
"'".mysql_real_escape_string(trim($_POST["zip"]))."', ".
"'".$promo."', ".
"'".mysql_real_escape_string(trim($_POST["phone"]))."',NOW(),'".trim(mysql_real_escape_string($_POST["country"]))."') ";
$rs= mysql_query($sql);
$uid = mysql_insert_id();
$_SESSION['uid'] = $uid;
$country = trim($_POST["country"]);
$_SESSION['country'] = trim($_POST["country"]);
if (mysql_affected_rows()<0){print "<B>Fatal ERROR: Could not add customer.</B>";exit();}};
if (isset($_POST['place'])) {$sql="SELECT * from cust where id = ".mysql_real_escape_string($_SESSION['uid']);
$rs= mysql_query($sql) ;
echo mysql_error();
$row=mysql_fetch_array($rs);
$eemail = $row["email"];
$country = $row["country"];
if ($_POST["express"] == "15"){$express = 15; $expyes = "yes";}else{$express = 0; $expyes = "no";}
$ordertotal = $_SESSION["tcost"] + $_SESSION["ship"] + $_SESSION["taxx"] + $express ;
$ordertotal = $_SESSION["tcost"] + $_SESSION["ship"] + $_SESSION["taxx"] + $express ;
$_SESSION["mthis"] ="" ;
$_SESSION["mthis"] .=$row["fname"]." ".$row["lname"]."\n" ;
$_SESSION["mthis"] .=$row["addr"]."\n".$row["city"].", ".$row["stat"].", ".$row["zip"].", "."\n" ;
$_SESSION["mthis"] .=$row["country"]."\n".$row["email"]."\n".$row["phone"]."\n"."\n"."Order info:"."\n" ;
$sql="INSERT INTO orders(cust_id, cart_total, taxx, shipping, express, s_first, s_last, s_addr, s_city, s_stat, s_zip, s_email, s_country, dt, s_phone) VALUES (".
"'".mysql_real_escape_string($row["id"])."', ".
"'".mysql_real_escape_string($ordertotal)."', ".
"'".mysql_real_escape_string($_SESSION["taxx"])."', ".
"'".mysql_real_escape_string($_SESSION["ship"])."', ".
"'".mysql_real_escape_string($expyes)."', ".
"'".mysql_real_escape_string($row["fname"])."', ".
"'".mysql_real_escape_string($row["lname"])."', ".
"'".mysql_real_escape_string($row["addr"])."', ".
"'".mysql_real_escape_string($row["city"])."', ".
"'".mysql_real_escape_string($row["stat"])."', ".
"'".mysql_real_escape_string($row["zip"])."', ".
"'".mysql_real_escape_string($row["email"])."', ".
"'".mysql_real_escape_string($row["country"])."',NOW(),'".mysql_real_escape_string($row["phone"])."') ";
$rs= mysql_query($sql,$o_conn) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
$oid = mysql_insert_id();
$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . mysql_real_escape_string( GetCartId() ) . "' order by items.itemName asc");
if (mysql_num_rows($result)==0){echo mysql_num_rows($result);}
while($row = mysql_fetch_array($result)){
$_SESSION["mthis"] .=$row["itemName"]." - ".$row["itemStyle"]."\n" ;
$_SESSION["mthis"] .="Size: ".$row["size"]."\n" ;
$_SESSION["mthis"] .="Color: ".$row["color"]."\n" ;
$_SESSION["mthis"] .="Qty: ".$row["qty"]."\n" ;
$_SESSION["mthis"] .="Gift wrap: ".ucwords($row["gift_wrap"])."\n" ;
$_SESSION["mthis"] .="Unit Price: ".$row["price"]."\n"."\n" ;
$sql="INSERT INTO order_details(o_id, prod_id, name, size, color, qty, gift_wrap, unitprice, total) VALUES (".
"'".mysql_real_escape_string($oid)."', ".
"'".mysql_real_escape_string($row["itemId"])."', ".
"'".mysql_real_escape_string($row["itemName"])."', ".
"'".mysql_real_escape_string($row["size"])."', ".
"'".mysql_real_escape_string($row["color"])."', ".
"'".mysql_real_escape_string($row["qty"])."', ".
"'".mysql_real_escape_string($row["gift_wrap"])."', ".
"'".mysql_real_escape_string($row["price"])."', ".
"'".mysql_real_escape_string($row["price"]*$row["qty"])."') ";
$rs= mysql_query($sql, $o_conn) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());}
$_SESSION["mthis"] .="Shipping = $ ".$_SESSION["ship"]."\n" ;
$_SESSION["mthis"] .="Express Shippig = $ ".$expyes."\n" ;
if ($taxx != ""){$_SESSION["mthis"] .="Tax = $ ".$taxx."\n" ;}
$_SESSION["mthis"] .="Total = $ ".$ordertotal."\n" ;
$_SESSION["mthis"] .="........................................";
$sendto = "Mitch3@me.com";
$subject = "Website Order";
$message = $_SESSION["mthis"];
$headers = 'From: order@' . "simplyjiang.com" . "\r\n" .
'Reply-To: order@' . "simplyjiang.com" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// send email
mail($sendto, $subject, $message, $headers);
$_SESSION["oid"] = $oid;
header("Location: trans/sim.php"); }?>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SimplyJiang - SHOP</title>
<link href="style.css" rel="stylesheet" type="text/css"><style type="text/css">
<!--
.style1 { font-family: "Times New Roman", Times, serif;
font-size: 12px;
color: #333333;
}
.style5 {color: #FFFFFF}
.style8 {font-size: 10px}
-->
</style>
<script language="javascript">
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}
function calcFees(chk,cost){
if (chk.checked == 1){
document.getElementById("stotal").innerHTML = formatCurrency(cost + 15);}
else
{
document.getElementById("stotal").innerHTML = formatCurrency(cost);}
}
</script>
</head>
<body>
<table width="800" height="298" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF">
<tr>
<td height="298" align="center" valign="top"><p><a href="index_09.html"></a></p>
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="200" height="25" align="center"><h3 align="center" class="style17">CHECKOUT </h3>
<p align="center" class="style1">Please verify the info <strong></strong> belo<span class="style42">w</span>: <br>
<br>
</p></td>
</tr>
</table>
<div align="center">
<form action="checkout2.php?" method="post">
<?php
function GetCartId(){
// This function will generate an encrypted string and
// will set it as a cookie using set_cookie. This will
// also be used as the cookieId field in the cart table
if(isset($_COOKIE["cartId"])){
return $_COOKIE["cartId"];
}
else{
// There is no cookie set. We will set the cookie
// and return the value of the users session ID
session_start();
setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));
return session_id();
}
}
$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . mysql_real_escape_string( GetCartId() ) . "' order by items.itemName asc");?>
<table width="80%" border="0" align="center" cellpadding="4" cellspacing="0" bgcolor="#CCCCCC">
<tr>
<td height="25" bgcolor="#999999" class="style1"><span class="style34 style37 style17 style5 style8">ITEM</span></td>
<td width="17%" height="25" bgcolor="#999999" class="style1"><span class="style34 style37 style17 style5 style8">COLOR</span></td>
<td height="25" bgcolor="#999999" class="style1"><span class="style34 style37 style17 style5 style8">QTY</span></td>
<td height="25" bgcolor="#999999" class="style1" align="center"><span class="style34 style37 style17 style5 style8">GiFT WRAP</span></td>
<td height="25" bgcolor="#999999" class="style1"><span class="style34 style37 style17 style5 style8">SUBTOTAL</span></td>
</tr>
<? if (mysql_num_rows($result)==0){echo mysql_num_rows($result);}
$_SESSION["mthis"] ="";while($row = mysql_fetch_array($result)){
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["price"]);
if ($sstat == "NY"){$taxx = round($totalCost * .08875,2);}?>
<tr>
<td width="57%" height="25" bgcolor="#FFFFFF" class="style1"><span class="style32 style8"><font face="verdana"> <?php echo $row["itemName"]; ?> - <font face="verdana"><?php echo $row["size"]; ?></font></font></span></td>
<td height="25" bgcolor="#FFFFFF" class="style1"><span class="style32 style8"><font face="verdana"><?php echo $row["color"]; ?></font></span></td>
<td width="11%" bgcolor="#FFFFFF" class="style1"><span class="style32 style8"><font face="verdana"> <?php echo $row["qty"]; ?> </font></span></td>
<td width="11%" bgcolor="#FFFFFF" class="style1" align="center"><span class="style32 style8"><font face="verdana"><?php if ($row["gift_wrap"] == 'yes'):?><img src="images/checked.gif" width="15" align="middle" /><?php endif;?></font></span></td>
<td width="15%" height="25" bgcolor="#FFFFFF" class="style1"><span class="style32 style8"><font face="verdana"> $<?php echo number_format($row["price"]*$row["qty"], 2, ".", ","); ?></font></span></td>
</tr>
<?php
}
//----------Domestic Shipping--------------//
if ($totalCost > 0 && $totalCost <= 100){$ship = 6.50;}
if ($totalCost > 100 && $totalCost <= 300){$ship = 10.50;}
if ($totalCost > 300 && $totalCost <= 500){$ship = 14.00;}
if ($totalCost > 500 && $totalCost <= 700){$ship = 18.00;}
if ($totalCost > 700 && $totalCost <= 1000){$ship = 20.00;}
if ($totalCost > 1000){$ship = 25.00;}
//----------International Shipping--------------//
if ($totalCost > 0 && $totalCost <= 100){$ship = 30.00;}
if ($totalCost > 100 && $totalCost <= 300){$ship = 35.00;}
if ($totalCost > 300 && $totalCost <= 500){$ship = 40.00;}
if ($totalCost > 500 && $totalCost <= 700){$ship = 45.00;}
if ($totalCost > 700 && $totalCost <= 1000){$ship = 50.00;}
if ($totalCost > 1000){$iship = 50.00;}
//----------store important values in session--------------//
$_SESSION['tcost'] = $totalCost;
$_SESSION['ship'] = $ship;
$_SESSION['taxx'] = $taxx;
//----------store important values in session--------------//?>
<tr>
<td colspan="5" bgcolor="#FFFFFF" class="style1"><div align="right">______________________</div></td></tr>
<tr>
<td colspan="4" bgcolor="#FFFFFF" class="style1"><div align="right" class="style17 style8"><span class="style39">TAX:</span></div></td>
<td bgcolor="#FFFFFF" class="style1"><span class="style32 style8"> <font face="verdana"><b>$<?php echo number_format($taxx, 2, ".", ","); ?></b></font></span></td></tr>
<tr>
<td colspan="4" bgcolor="#FFFFFF" class="style1"><div align="right" class="style17 style8"><span class="style39">SHIPPING:</span></div></td>
<td bgcolor="#FFFFFF" class="style1"><span class="style32 style8"> <font face="verdana"><b>$<?php echo number_format($ship, 2, ".", ","); ?></b></font></span></td></tr>
<tr>
<td colspan="4" bgcolor="#FFFFFF" class="style1"><div align="right" class="style17 style8"><span class="style39">EXPRESS DELIVERY:* </span></div></td>
<td bgcolor="#FFFFFF" class="style1"><input name="express" type="checkbox" id="express" onClick="calcFees(express,<?=$totalCost + $ship + $taxx;?>);" value="15"/></td></tr>
<tr>
<td colspan="4" bgcolor="#FFFFFF" class="style1"><div align="right" class="style17 style8"><span class="style39">TOTAL:</span></div></td>
<td bgcolor="#FFFFFF" class="style1"><span class="style32 style8"><font face="verdana"><b>
<div id="stotal">$<?php echo number_format($totalCost + $ship + $taxx, 2, ".", ","); ?></div>
</b></font></span></td></tr>
</table>
<input name="place" type="hidden" id="place" value="yes" />
<input name="tcost" type="hidden" id="tcost" value="<? echo $totalCost; ?>" />
<input name="ship" type="hidden" id="ship" value="<? echo $ship; ?>" />
<input name="taxx" type="hidden" id="taxx" value="<? echo $taxx; ?>" />
<input type="submit" name="Submit" value="Place Order" />
</p>
</form></div></td></tr></table></body></html>