cookies and form elements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tarrigo
Forum Newbie
Posts: 8
Joined: Sat Nov 25, 2006 4:48 pm

cookies and form elements

Post by tarrigo »

I have just started programming in PHP and have the basics down, but what has always been difficult for me is how to properly store products in a cookie. I could store users selections in a database and create a unique cookie and id for that customer and retrieve values, but I really want to use cookies to track a customers wishlist as they surf around my web site.

So imagine I have a page with 10 products listed. I want the user to be able to hit checkboxes that represent products and then have the next submit page take the values and add them into a cookie. But I then want to be able to use the unique product id's that were checked to go and query the MYSQL database and list the products. The database part I can do, but I can't seem to figure out how to take the form items that get submitted to the processing page to do what I want them to do. When the data gets submitted to the processing page the querystring looks like http://thewebsiteexample/cart.php?item=1&item=2&item=3. I think you get the point. In ASP it would just take the values and combine them and comma dilineate them and I could use that in my sql query, but I can't seem to do that in PHP. So that is why I am asking for some assistance or some thoughts on possible other ways to store theoretical products in a cookie.

The HTML page that I use to submit the values to the processing page looks like the following below. It represents a page that asks the user to checkoff products they want to add to their cart. Just so you could get an example of what kind of data is being submitted over. Any help or theories would be great.

<input type='checkbox' value='12393' name='item'>
<input type='checkbox' value='125553' name='item'>
<input type='checkbox' value='127' name='item'>
<input type='checkbox' value='00234' name='item'>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

<html
	<head><title>checkbox/array test</title></head>
	<body>
<?php
if ( isset($_GET['item']) && is_array($_GET['item']) ) {
	echo "		<fieldset><legend>selected items</legend>\n";
	foreach( $_GET['item'] as $item ) {
		echo '			selected: ', htmlentities($item), "<br />\n";
	}
	echo "		</fieldset>\n";
}
?>
		<form method="get" action="#">
			<div>
				<input type="checkbox" name="item[]" value="12393" />12393<br />
				<input type="checkbox" name="item[]" value="125553" />125553<br />
				<input type="checkbox" name="item[]" value="127" />127<br />
				<input type="checkbox" name="item[]" value="00234" />00234<br />
				<input type="submit" />
			</div>
		</form>
	</body>
</html>
the trick is name="item[]".
Post Reply