arunpatal Posted January 16, 2013 Share Posted January 16, 2013 Hi, I have page call a.php with 2 form in it..... <form action="b.php"> Item 1: <input type="text" name="item1"> <input type="submit" value="add"> </form> <form action="b.php"> Item 2: <input type="text" name="item2"> <input type="submit" value="add"> </form> Now it send variable to page b.php <?php session_start(); ?> <?php $_SESSION['item1'] = $_GET['item1']; echo 'you have added ' . $_SESSION['item1']; ?> how can i do this? If i type computer in text field item1 then in page b.php it store the value as computer and when i go back and type laptop in text field item2 then page b.php store this value also.. So the display of b.php page will be with two items in it. Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/ Share on other sites More sharing options...
Jessica Posted January 16, 2013 Share Posted January 16, 2013 You're probably going to want to use an array, if I understand your goal. Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/#findComment-1406008 Share on other sites More sharing options...
arunpatal Posted January 16, 2013 Author Share Posted January 16, 2013 You're probably going to want to use an array, if I understand your goal. Can you show me by editing the code above to explain it in more detail... Please Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/#findComment-1406009 Share on other sites More sharing options...
Christian F. Posted January 16, 2013 Share Posted January 16, 2013 Give the input fields names like this: <input type="text" name="item[1]" value="something"> Then add this to the top of your receiving script, to see how it will look like after submission: echo "<pre>";var_dump ($_POST);echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/#findComment-1406047 Share on other sites More sharing options...
arunpatal Posted January 16, 2013 Author Share Posted January 16, 2013 Give the input fields names like this: <input type="text" name="item[1]" value="something"> Then add this to the top of your receiving script, to see how it will look like after submission: echo "<pre>";var_dump ($_POST);echo "</pre>"; Hi, i tried like this page a.php <form action="b.php"> Item 1: <input type="text" name="item[1]" value="1"> <input type="submit" value="add"> </form> <form action="b.php"> Item 2: <input type="text" name="item[2]" value="2"> <input type="submit" value="add"> </form> page b.php <?php session_start(); ?> <?php echo "<pre>";var_dump ($_POST);echo "</pre>"; ?> But the output is array(0) { } Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/#findComment-1406105 Share on other sites More sharing options...
Jessica Posted January 16, 2013 Share Posted January 16, 2013 His form is using GET not POST. Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/#findComment-1406106 Share on other sites More sharing options...
arunpatal Posted January 16, 2013 Author Share Posted January 16, 2013 His form is using GET not POST. Ok i change the form like this <form action="b.php" method="post"> Item 1: <input type="text" name="item[1]" value="1"> <input type="submit" value="add"> </form> <form action="b.php" method="post"> Item 2: <input type="text" name="item[2]" value="2"> <input type="submit" value="add"> </form> now the out put shows like this array(1) { ["item"]=> array(1) { [2]=> string(6) "Laptop" } } I want to display all items name which i already added....... Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/#findComment-1406107 Share on other sites More sharing options...
DavidAM Posted January 16, 2013 Share Posted January 16, 2013 You have two separate forms. Only one FORM will be submitted, and only the fields in that one FORM will be submitted. Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/#findComment-1406108 Share on other sites More sharing options...
arunpatal Posted January 16, 2013 Author Share Posted January 16, 2013 You have two separate forms. Only one FORM will be submitted, and only the fields in that one FORM will be submitted. But i want to add different items... just like shopping cart Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/#findComment-1406109 Share on other sites More sharing options...
Jessica Posted January 16, 2013 Share Posted January 16, 2013 You can have two elements in one form. However, if you want them to click the buttons separately for each, then it was working. You need to more clearly explain your problem because I think it is very different from the way others are interpreting it. I was suggesting using an array in your session. IE, whenever a form is posted and you have a new item, add it to your $_SESSION['cart'] array. There are a lot of tutorials out there on shopping carts. Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/#findComment-1406112 Share on other sites More sharing options...
arunpatal Posted January 16, 2013 Author Share Posted January 16, 2013 You can have two elements in one form. However, if you want them to click the buttons separately for each, then it was working. You need to more clearly explain your problem because I think it is very different from the way others are interpreting it. I was suggesting using an array in your session. IE, whenever a form is posted and you have a new item, add it to your $_SESSION['cart'] array. There are a lot of tutorials out there on shopping carts. i looked around and found some shopping cart tutorials but they are very complicated. I am just looking a simple example via which i can add multi item to session. Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/#findComment-1406114 Share on other sites More sharing options...
Jessica Posted January 16, 2013 Share Posted January 16, 2013 So where is the problem? Do you want to add them one at a time by pressing many buttons, or all at once? Don't forget to have session start on EVERY page Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/#findComment-1406116 Share on other sites More sharing options...
arunpatal Posted January 16, 2013 Author Share Posted January 16, 2013 (edited) So where is the problem? Do you want to add them one at a time by pressing many buttons, or all at once? Don't forget to have session start on EVERY page When i add item from from 1.... it display the item like this array(1) { ["item"]=> array(1) { [2]=> string( "Computer" } } and when i go back to 1st page and add item from form 2 then the 1st item from from 1 is not there... i want to display multi items from different forms This is my form page <?php session_start(); ?> <form action="b.php" method="post"> Item 1: <input type="text" name="item[1]" value="1"> <input type="submit" value="add"> </form> <form action="b.php" method="post"> Item 2: <input type="text" name="item[2]" value="2"> <input type="submit" value="add"> </form> and this is the page which receive items <?php session_start(); ?> <?php echo var_dump ($_POST); ?> Edited January 16, 2013 by arunpatal Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/#findComment-1406121 Share on other sites More sharing options...
Jessica Posted January 16, 2013 Share Posted January 16, 2013 How do you know it's not there? Your code now is only showing the post array not session. I'm on the phone when I get back to my desk I will type an example. Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/#findComment-1406124 Share on other sites More sharing options...
arunpatal Posted January 16, 2013 Author Share Posted January 16, 2013 How do you know it's not there? Your code now is only showing the post array not session. I'm on the phone when I get back to my desk I will type an example. am waiting... Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/#findComment-1406125 Share on other sites More sharing options...
Jessica Posted January 16, 2013 Share Posted January 16, 2013 (edited) Okay here is a.php. <?php session_start(); ?> <form action="b.php" method="post"> Item 1: <input type="text" name="item[1]"> <input type="submit" value="add"> </form> <form action="b.php" method="post"> Item 2: <input type="text" name="item[2]"> <input type="submit" value="add"> </form> <?php print '<pre>'.print_r($_SESSION, true).'</pre>'; And here is b.php <?php session_start(); if(isset($_POST['item'])){ foreach($_POST['item'] AS $key=>$item){ $_SESSION['cart'][$key] = $item; } } print '<pre>'.print_r($_SESSION, true).'</pre>'; echo '<a href="a.php">Back to a.php</a>'; ?> Now try this and see what your print_r of session is after adding product 1, then product 2, and going back to a.php each time. This code is not tested. I used the foreach in b.php so you could do multiple products at once. If you are never going to do multiple products at once you could have 1 text input with just "item" as the name, and another hidden input with the id if you need it. The processing would have to change. Edited January 16, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/#findComment-1406128 Share on other sites More sharing options...
arunpatal Posted January 16, 2013 Author Share Posted January 16, 2013 Okay here is a.php. <?php session_start(); ?> <form action="b.php" method="post"> Item 1: <input type="text" name="item[1]"> <input type="submit" value="add"> </form> <form action="b.php" method="post"> Item 2: <input type="text" name="item[2]"> <input type="submit" value="add"> </form> <?php print '<pre>'.print_r($_SESSION, true).'</pre>'; And here is b.php <?php session_start(); if(isset($_POST['item'])){ foreach($_POST['item'] AS $key=>$item){ $_SESSION['cart'][$key] = $item; } } print '<pre>'.print_r($_SESSION, true).'</pre>'; echo '<a href="a.php">Back to a.php</a>'; ?> Now try this and see what your print_r of session is after adding product 1, then product 2, and going back to a.php each time. This code is not tested. I used the foreach in b.php so you could do multiple products at once. If you are never going to do multiple products at once you could have 1 text input with just "item" as the name, and another hidden input with the id if you need it. The processing would have to change. The code works... now i will try to understand the code Thanks Quote Link to comment https://forums.phpfreaks.com/topic/273219-adding-items-to-session/#findComment-1406129 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.