<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  

Recommended Answers

All 7 Replies

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']);
}
?>
<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

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

An remove this line:

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

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

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>

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.