sijis
10-20-2003, 10:11 PM
Specifically, i belive my problem is in get_saved_cart() function. returns undefined
Or it could be with funciton GetCookie() which is used in get_saved_cart(). I got all the cookie functions from a the internet. Either way, any help would be appreciated.
Code below:
<script language="JavaScript">
<!--
/*
## cookie base functions ##
*/
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1){
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
}
function GetCookie(name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg){
return getCookieVal (j);
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0){
break;
}
}
return null;
}
function SetCookie(name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie(name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
//my code.
function calculate_item(){
var quant = document.item_form.quant.value;
var cost = document.item_form.item_cost.value;
var total = quant*cost;
document.item_form.total.value = total;
return true;
}
function save_cart_to_cookie() {
var expdate = new Date();
expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000 * 31));
var item_info = new Array();
item_info[0] = document.item_form.item_id.value;
item_info[1] = document.item_form.quant.value;
item_info[2] = document.item_form.item_cost.value;
item_info[3] = document.item_form.total.value;
if(item_info[1]*item_info[2] != item_info[3]){
alert('Totals dont add up');
}
else{
if (confirm('Save to cart?')) {
SetCookie('cart', item_info, expdate);
}
}
}
function get_saved_cart() {
//alert(getCookieVal('cart'));
if(GetCookie('cart') != null){
//alert('Cart total ' + GetCookie('cart') + '.');
var text;
var cookinfo = getCookieVal('cart');
for(var key in cookinfo){
text += key + "=" + escape( cookinfo[key] ) + "&";
}
alert(text);
}
}
//-->
</script>
<form name="item_form" id="item_form" method="post" action="">
<table width="75%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Quantity:</td>
<td><input name="quant" type="text" id="quant" value="1" /></td>
</tr>
<tr>
<td>Total:</td>
<td><input name="total" type="text" id="total" /></td>
</tr>
<tr>
<td><input name="item_cost" type="hidden" id="item_cost" value="9.99" />
<input name="item_id" type="hidden" id="item_id" value="1" />
<input name="item_total" type="hidden" id="item_total" /></td>
<td><input name="recalculate" type="button" id="recalculate" value="Recalculate Total" onclick="calculate_item();" />
<input name="add_to_cart" type="button" id="add_to_cart" value="Add" onclick="save_cart_to_cookie();" />
<input name="get_cart" type="button" id="get_cart" value="Get Cookie Value" onclick="get_saved_cart();" />
</td>
</tr>
</table>
</form>
Or it could be with funciton GetCookie() which is used in get_saved_cart(). I got all the cookie functions from a the internet. Either way, any help would be appreciated.
Code below:
<script language="JavaScript">
<!--
/*
## cookie base functions ##
*/
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1){
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
}
function GetCookie(name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg){
return getCookieVal (j);
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0){
break;
}
}
return null;
}
function SetCookie(name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie(name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
//my code.
function calculate_item(){
var quant = document.item_form.quant.value;
var cost = document.item_form.item_cost.value;
var total = quant*cost;
document.item_form.total.value = total;
return true;
}
function save_cart_to_cookie() {
var expdate = new Date();
expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000 * 31));
var item_info = new Array();
item_info[0] = document.item_form.item_id.value;
item_info[1] = document.item_form.quant.value;
item_info[2] = document.item_form.item_cost.value;
item_info[3] = document.item_form.total.value;
if(item_info[1]*item_info[2] != item_info[3]){
alert('Totals dont add up');
}
else{
if (confirm('Save to cart?')) {
SetCookie('cart', item_info, expdate);
}
}
}
function get_saved_cart() {
//alert(getCookieVal('cart'));
if(GetCookie('cart') != null){
//alert('Cart total ' + GetCookie('cart') + '.');
var text;
var cookinfo = getCookieVal('cart');
for(var key in cookinfo){
text += key + "=" + escape( cookinfo[key] ) + "&";
}
alert(text);
}
}
//-->
</script>
<form name="item_form" id="item_form" method="post" action="">
<table width="75%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Quantity:</td>
<td><input name="quant" type="text" id="quant" value="1" /></td>
</tr>
<tr>
<td>Total:</td>
<td><input name="total" type="text" id="total" /></td>
</tr>
<tr>
<td><input name="item_cost" type="hidden" id="item_cost" value="9.99" />
<input name="item_id" type="hidden" id="item_id" value="1" />
<input name="item_total" type="hidden" id="item_total" /></td>
<td><input name="recalculate" type="button" id="recalculate" value="Recalculate Total" onclick="calculate_item();" />
<input name="add_to_cart" type="button" id="add_to_cart" value="Add" onclick="save_cart_to_cookie();" />
<input name="get_cart" type="button" id="get_cart" value="Get Cookie Value" onclick="get_saved_cart();" />
</td>
</tr>
</table>
</form>