hello everyone,

i'm new to php and i'm having hard time with sessions

i'm trying to create a php file with a drop down menu and when you select an item from the drop down menu, you could retreve it from another page.

for example:

a1.php

<?php session_start();
	if(isset($_POST['color'])) 
	{ 
		$_SESSION['blue']='blue'; 
		$_SESSION['red']='red'; 
		$_SESSION['green']='green'; 
		$_SESSION['orange']='orange'; 
	} 
	?>
<html>
	<body>	
		<form id="shirt" method="post" action="a2.php">
			<p>
				<select name="Size">
					<option value="invalid">Select a size ...</option>
					<option value="blue">blue</option>
					<option value="red">red</option>
					<option value="green">green</option>
					<option value="orange">orange</option>
				</select>
				<br />
				<input type="Submit" value="Add" name="Add" />
			</p>
		</form>
	</body>
</html>

when the user chooses a color, it adds it to the session and then when the user clicks add, he is redirected to another page named a2.php which shows the color is added.

if the user goes back to the original page and adds the same color again it shows that he added the item again:

Color: ----------- Quantity:
Red ----------- 2

a2.php

<?php session_start();
	$item_id = $_GET[id];
	$action 	= $_GET[action];

	switch($action)
	{	
	
		case "add":
			$_SESSION['color'][$item_id]++;
		break;
		
		case "remove":
			$_SESSION['color'][$item_id]--; 

	if($_SESSION['color'][$item_id] == 0) unset($_SESSION['color'][$item_id]);  
		break;
		
		case "empty":
			unset($_SESSION['color']);
		break;
	
	}
?>

sorry if my question is not clear,


any help is appreciated

Thank You,

Recommended Answers

All 4 Replies

Your are using method=post in a1.php and your try to fetch values with $_GET[] in a2.php.... This wont work. You should use $_POST[] in a2.php instead.

so you're saying it should stay the same except change the get to post

<?php session_start();
	$item_id = $_POST[id];
	$action 	= $_POST[action];

	switch($action)
	{	
	
		case "add":
			$_SESSION['color'][$item_id]++;
		break;
		
		case "remove":
			$_SESSION['color'][$item_id]--; 

	if($_SESSION['color'][$item_id] == 0) unset($_SESSION['color'][$item_id]);  
		break;
		
		case "empty":
			unset($_SESSION['color']);
		break;
	
	}
?>

like this?


sorry i'm slow at learning this :(

uuuuuuuuup

uuuuuuuuuuuup

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.