0

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,

Edited by david_s: n/a

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by david_s
0

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.

Edited by larsmw: n/a

0

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 :(

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.