david_s Posted April 4, 2011 Share Posted April 4, 2011 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, Quote Link to comment Share on other sites More sharing options...
requinix Posted April 5, 2011 Share Posted April 5, 2011 Your session stuff looks fine. It's the rest that's a problem. 1. Your form uses POST but your code assumes GET. 2. Strings need quotes. Even if you're using them as array keys. 3. There is no id or action in your form. They are attributes of the HTML tag, but (a) those aren't submitted and (b) they aren't even what you want. You want "Size" and "Add". 4. There is no $_SESSION["color"]. There's ["blue"] and ["red"] and ["green"] and ["orange"] but no ["color"]. 5. $_SESSION[whatever] isn't a number. It's a string. Incrementing and decrementing a string doesn't make sense. Get a piece of paper (or fire up your favorite word processor or text editor) and decide what you want the form to do. Where you want to store variables. What they're going to be called. Then write your code, using that paper (or document) as a guide for what you should do. Quote Link to comment Share on other sites More sharing options...
david_s Posted April 5, 2011 Author Share Posted April 5, 2011 thank you for the reply, this is the changes i made in the last few minutes: <?php session_start(); // configure colors $colors= array("S", "M", "L", "XL"); // Store the count of each color foreach ($colors as $color) { // IF the form as submitted do stuff to ++ this color $_SESSION[$color]++; // This is $_SESSION['blue']=1; the first time this loop runs } ?> <html> <body> <form id="shirt" method="post" action="a2.php"> <p> <select name="color"> <option value="invalid">Select a color ...</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> i don't know about a2.php, do you think i have the right code? Quote Link to comment Share on other sites More sharing options...
david_s Posted April 5, 2011 Author Share Posted April 5, 2011 uuuuuuuuuuuuuuup Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
× Pasted as rich text. Restore formatting
Only 75 emoji are allowed.
× Your link has been automatically embedded. Display as a link instead
× Your previous content has been restored. Clear editor
× You cannot paste images directly. Upload or insert images from URL.