[FormFields]
WB_UseSessions=T
#* I am setting WB_UseSessions to True so I can use sessions with WhizBase *#
<!--WB_BeginTemplate-->
#* I am checking if product1,product2 and product3 variables are not set and set a value of zero, that is how I know if the user is comming for the first time or not.*#
$wbif["$wbgets[product1]"=""|$wbsets[product1|0|f]|]$wbif["$wbgets[product2]"=""|$wbsets[product2|0|f]|]$wbif["$wbgets[product3]"=""|$wbsets[product3|0|f]|]
#* I am using $wbgets[varname] to get a session variable, and $wbsets[varname€|value] to set a session variable *#
<html>
<head>
<script type="text/javascript">
#* I am making a showCart JavaScript/AJAx function, it creates an AJAX object and calls «showCart.wbsp» file to show the current status of the cart, the retrieved data is putted by innerHTML into «cartview» div *#
function showCart()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("cartview").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","showCart.wbsp?rand="+Math.floor(Math.random()*99999999999),true);
xmlhttp.send();
}
#* addCart function in JavaScript will pass an ID and send it to addCart.wbsp which adds a new product in the cart. On responce we will refresh the cart view by calling showCart function*#
function addCart(id)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
showCart();
}
}
xmlhttp.open("GET","addCart.wbsp?id="+id+"&rand="+Math.floor(Math.random()*99999999999),true);
xmlhttp.send();
}
#* remove function in JavaScript will pass an ID and send it to removeCart.wbsp which removes one product passed from the cart. On responce we will refresh the cart view by calling showCart function *#
function remove(id)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
showCart();
}
}
xmlhttp.open("GET","removeCart.wbsp?id="+id+"&rand="+Math.floor(Math.random()*99999999999),true);
xmlhttp.send();
}
</script>
</head>
<body onload="showCart();">
#* OnLoad we will refresh the cart by showCart() function, this will update the cart div every time we refresh the page *#
#* We list the products and every product have its own ID *#
<a href="javascript:void(0);" onclick="addCart(1);">Product 1</a><br />
<a href="javascript:void(0);" onclick="addCart(2);">Product 2</a><br />
<a href="javascript:void(0);" onclick="addCart(3);">Product 3</a><br /><br /><br />
<b>Cart content</b><br />
#* cartview is a div for holding cart data.*#
<div id="cartview">Cart is empty!</div>
</body>
</html>
[FormFields]
WB_UseSessions=T
#* I am turning the sessions to True *#
<!--WB_BeginTemplate-->
$wbif["$wbv[id]"="1"|$WBSETS[product1|$WBCalc[$WBGETS[product1]+1]|f]|]
$wbif["$wbv[id]"="2"|$WBSETS[product2|$WBCalc[$WBGETS[product2]+1]|f]|]
$wbif["$wbv[id]"="3"|$WBSETS[product3|$WBCalc[$WBGETS[product3]+1]|f]|]
#* I have three cases in my example so I am hard-coding them. I check the ID variable sent by GET or POST by $WBV, if it is equal an ID I have I will set a value of the variable+1 using $WBCalc, $WBSets and $WBGets, if not do nothing *#
[FormFields]
WB_UseSessions=T
#* I am turning the sessions to True *#
<!--WB_BeginTemplate-->
$wbif["$wbv[id]"="1"|$WBSETS[product1|$WBCalc[$WBGETS[product1]-1]|f]|]
$wbif["$wbv[id]"="2"|$WBSETS[product2|$WBCalc[$WBGETS[product2]-1]|f]|]
$wbif["$wbv[id]"="3"|$WBSETS[product3|$WBCalc[$WBGETS[product3]-1]|f]|]
#* Removing is the same code as adding but the only different we will remove an item not adding it. *#
[FormFields]
WB_UseSessions=T
#* I am turning the sessions to True *#
<!--WB_BeginTemplate-->
There is:
<br />$wbgets[product1] X Product1 $wbif[$wbgets[product1]>0|(<a href="javascript:void(0);" onclick="remove(1);">remove</a>)|] #* we show the session variable containning number of items of the products and only if there is items then show the remove link also *#
<br />$wbgets[product2] X Product2 $wbif[$wbgets[product2]>0|(<a href="javascript:void(0);" onclick="remove(2);">remove</a>)|]
<br />$wbgets[product3] X Product3 $wbif[$wbgets[product3]>0|(<a href="javascript:void(0);" onclick="remove(3);">remove</a>)|]<br />
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Author
Commented: