Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/273219-adding-items-to-session/
Share on other sites

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>";

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) {

}

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.......

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.

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.

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 by arunpatal

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 by Jessica

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... :happy-04:

now i will try to understand the code :confused:

Thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.