PDA

View Full Version : Dealing with Ajax Data in more then just <div>



caleman
05-06-2009, 03:00 AM
So I am trying to build a dynamic order total

subtotal = 12.00
tip 1.00 = 1.00
coupon code 10%off =1.20 <coupon code is 10% off>

total = 11.80

red is input fields

so using the Ajax system I can go lookup the the 10%off via a PHP sql call and it will return that it is 10% off the subtotal.

Coupon: <input name="coupon_code" type="text" onKeyDown="showCoupon(this.value,'txtHintB',this.form.total.value)" size="6" maxlength="6"> <span class="style7"><div id="txtHintB"></div></span><br>

I get back the discount value and I can pass that back into the page just fine but ... How do I get the total to update based on the discount that was returned?

my ajax code

function showCoupon(str,myloc,ordertotal)
{
//alert ("str is "+str+" myloc is "+myloc+" the order total is "+ordertotal);

xmlHttp=GetXmlHttpObject();
//alert("getxmlhttpob is "+xmlHttp);
if (xmlHttp==null)
{
//alert ("Your browser does not support AJAX!");
return;
}
var url="getcoupon.php";
url=url+"?coupon_code="+str;
url=url+"&order_total="+ordertotal;
url=url+"&sidd="+Math.random();
xmlHttp.onreadystatechange=function(){stateChangedLoc(myloc)};
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function stateChangedLoc(divname)
{

if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById(divname).innerHTML=xmlHttp.responseText;
}
}

and here is my php code for the lookup

if( ($_GET[coupon_code]) != "") {
//echo " total order is: $_POST[order_total] <br>";
$sql01 = "SELECT coupons.coupon_id, coupons.provider_id, coupons.percent_off, coupons.dollar_off, coupons.coupon_type, coupons.min_purchase, coupons.coupon_code
FROM
coupons
WHERE
provider_id = 1 AND
coupons.expiration_date > now() AND
coupons.coupon_code = '$_GET[coupon_code]'";

$output01 = mysql_query($sql01, $db_conn) or die (mysql_error()."<br />Couldn't execute query: $sql01");
$count01 = mysql_num_rows($output01);
$row01 = mysql_fetch_array($output01);
if ($count01 == 0) {
$coupon_error = "The Coupon, $_GET[coupon_code], has expired or does not exist";
$update = "coupon error";
} else {
if ($row01[min_purchase] > (int)$_GET[order_total]) {
//echo " $row01[min_purchase] is greater then $_POST[order_total] <br>";
$update = "coupon error";
$coupon_error = "The Order does not meet the minimum amount for this coupon";
}
}
if (empty($coupon_error)) {
if ($row01[percent_off] != 0) {
$coupon_discount = (($row01[percent_off] * .01) * $_GET[order_total]);
$coupon_id = $row01[coupon_id];
//echo " coupon is: $coupon_discount <br>";
} else {
$coupon_discount = $row01[dollar_off] ;
$coupon_id = $row01[coupon_id];
//echo " coupon is: $coupon_discount <br>";
}
} else {
echo "$coupon_error";
}
echo " " . number_format($coupon_discount,2). " ";
} // end of $_GET[coupon]