0
<html>
<body>
<form method="post" action="">
<input type="text" name="customername" required/>
<input type="text" name="productname" required/>
<input type="text" name="units" required/>
<input type="text" name="price" required/>
<input type="submit" value="submit"/>
</form>
<?php
if($_POST)
{
$_SESSION['array']=array();
array_push($_SESSION['array'],($_POST));
print_r($_SESSION['array']);
}
?>
</body>
</html>

Here My Need Was i Want To Save the all data which was submitted by the user.but here it was
replacing existing data and displaying the last posted data.
Can u please help me to achieve It.
I want to save all the data upto sessions ends which was posted by the user  
3
Contributors
7
Replies
20
Views
4 Years
Discussion Span
Last Post by masterjiraya
0

In PHP code use session_start() on the beginning to use a session.

<?php
session_start();
if($_POST)
{
    $_SESSION['array']=array();
    array_push($_SESSION['array'],($_POST));
    print_r($_SESSION['array']);
}
?>

Edited by broj1

0
<html>
<body>
<form method="post" action="">
<input type="text" name="customername" required/>
<input type="text" name="productname" required/>
<input type="text" name="units" required/>
<input type="text" name="price" required/>
<input type="submit" value="submit"/>
</form>
<?php
if($_POST)
{
$_SESSION['array']=array();
array_push($_SESSION['array'],($_POST));
print_r($_SESSION['array']);
}
?>
</body>
</html>

here's your code.
here's my code

<body>
    <form method="post" action="<?php htmlspecialchars($_SERVER['PHP_SELF'])?>">
    <input type="text" name="customername" required/>
    <input type="text" name="productname" required/>
    <input type="text" name="units" required/>
    <input type="text" name="price" required/>
    <input type="submit" value="submit"/>
    </form>
    <?php
    $customer=$_POST['customername'];
    $product=$_POST['productname'];
    $units=$_POST['units'];
    $price=$_POST['price'];
    $combine=$customer.", ".$product.", ".$units.", ".$price;
    if($_POST)
    {
    $_SESSION['array']=array();
    array_push($_SESSION['array'],$combine);
    print_r($_SESSION['array']);
    }
    ?>
    </body>
    </html>

array_push cannot handle the $_POST which handles more than one value. it onlyu handles one value per transaction so it needs to be in this form

Edited by masterjiraya

0

array_push cannot handle the $_POST which handles more than one value. it onlyu handles one value per transaction so it needs to be in this form

@masterjiraya: can you explain above statement. $_POST is an array and array_push is defined as:

int array_push ( array &$array , mixed $var [, mixed $... ] )

where $var is of mixed type (it can also be an array).

0

An remove this line:

$_SESSION['array']=array();

You do not want to initialize array each time if you want to append to it.

0

This is the complete code that works fine for me:

<html>
<body>
<form method="post" action="">
<input type="text" name="customername" required/>
<input type="text" name="productname" required/>
<input type="text" name="units" required/>
<input type="text" name="price" required/>
<input type="submit" value="submit"/>
</form>
<?php
session_start();
if($_POST)
{
    array_push($_SESSION['array'],($_POST));  
    print_r($_SESSION['array']);
}
?>
</body>
</html>
0

@broj1
I assume that he is handling one value in the case he use of array_push() so $_POST cannot be used there because $_POST in this case is handling all the fields in to an array behavior. not unless he pursue to insert an array inside an array element.

0

@broj1
there's nothing to be a problem with $_POST to the error he states above. but it will time consuming for computation

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.