Page 1 of 1

how to create cookie for a shopping cart.

Posted: Mon May 17, 2010 1:32 am
by amitdubey2
hello all,

I am making a shopping cart using javascript and php.I have a problem ....when i have already added a item to shopping cart basket and want to add a new products in the basket...the product which was previously added is not shown in the basket..
how to came out this problem..???
I am using cookie functions to store the values for the item selected to add in the basket...but not getting the right output..
here is the functions for cookies ..which i am using..(i want to store id, name,quantity,price and expiry date of products in cookie..

Code: Select all

function createCookie(id,name,qt,price,days) 
              {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = id+name+qt+price+expires+"; path=/";
}

function getCookie(id,name,qt,price) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function delCookie(name) {
document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/";
} 

And here is the page where i want to fetch the values which are added in the basket previously.

Code: Select all

<?php
$price= $_GET['price']; //i am getting these values through url (using window.location)any other method to send these values???
$code= $_GET['code'];
$name= $_GET['name'];
$qt= $_GET['qt'];
echo"<table border ='7' cellpadding='10' cellspacing = '10' bgcolor='pink'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Quantity</th>
<th>Price</th>
<tr>";
echo"<tr>";
echo"<td>" . $code . "</td>";
echo"<td>" . $name . "</td>";
echo"<td>";
?>
<form action="" method="post">
<input type="text" name="qt" value="<?php echo $qt ?>" size="2" >
</form>

<?php
echo"</td>";
echo"<td>" . $price . "</td>";
?>
<script type="text/javascript" src="translate.js"></script>
<script language="javascript">



createCookie("name","<?php echo $name ?>"); //here i m getting only current value for $name.
var x = document.cookie;
document.write(x);



</script>

Any suggetion regarding this.??? :dubious: :dubious:

Thanks



regards
amit

Re: how to create cookie for a shopping cart.

Posted: Mon May 17, 2010 1:46 am
by Apollo
First of all you cannot set cookies after you've already echo'ed or printed regular output. Cookies are part of the http header, which need to be sent before any other (html) output.

Furthermore, it looks quite messy altogether. For example, why the hell do you use so much interleaved php and direct output.. first you're echo'ing html, then you're closing php with ?> and including direct html with some inline <?php echo ?> statements (instead of just echo'ing everything), which doesn't exactly make things easier. And your function 'createCookie' appears to expect some of the parameters id, name, qt, price, and days to have separation tokens (; and/or =) already?