adding session

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
kpraman
Forum Contributor
Posts: 172
Joined: Fri Oct 13, 2006 10:54 am

adding session

Post by kpraman »

I am trying to add session to shopping cart. This is the code i am using, it is not working. The values gets lost once i add another product.

Code: Select all

if (!isset($_SESSION['checkbox']) || !is_array($_SESSION['checkbox'])) 
		{ 
			$_SESSION['checkbox'] = array(); 
		} 
		$_SESSION['checkbox'][] = $_POST['checkbox'];
		$_SESSION['checkbox'];
		
		print_r($checkbox);
checkbox contains prodId's
mentor
Forum Contributor
Posts: 100
Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan

Re: adding session

Post by mentor »

kpraman wrote:checkbox contains prodId's
Which variable you are talking about? $checkbox or $_POST['checkbox']?

Code: Select all

if (!isset($_SESSION['checkbox']) || !is_array($_SESSION['checkbox'])) 
{ 
	$_SESSION['checkbox'] = array(); 
} 
$_SESSION['checkbox'][] = $_POST['checkbox']; 
$_SESSION['checkbox']; // what is the need for this line?

print_r($checkbox);
Isn't it should be like

Code: Select all

print_r($_SESSION['checkbox']);
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Absolutely no idea what you are trying to accomplish here. Are you saving which checkboxes are checked for later usage...?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You need to approach this differently.

Code: Select all

<?php
if (!empty($_POST['checkbox']))
{
    $_SESSION['checkbox'] = $_POST['checkbox'];
}
?>
Post Reply