user2035681
user2035681

Reputation: 21

use php session to list multiple form input

I am new to php. How can I use session to remember my multiple form input like this: In the first webpage, I can input for example a product name, when I click submit, the second page can tell me the product name I typed in. AND, if I go back to the first page again, and input another product name, and click submit, the second page can display the first product and the new product. If I input more product names continuously in this way, I can see all products listed in the second page. How can I use session to do this?

First page (order.html):

<html><body>
<h4>Order Form</h4>
<form action="showorder.php" method="post"> 
Product name: <input name="prodname" type="text" /> 
<input type="submit" />
</form>
</body></html>

Second page (showorder.php):

<html><body>
<?php
$Pname = $_POST['prodname'];
echo "You ordered ". $Pname . ".<br />";
?>
</body></html>

Upvotes: 2

Views: 902

Answers (2)

Ynhockey
Ynhockey

Reputation: 3932

You can use the $_SESSION array in the following fashion:

$_SESSION['products'][] = $_POST['prodname'];

That means that $_SESSION['products'] will be an array containing all the purchased products.

Don't forget to call session_start() in the beginning of all relevant pages, and filter your code against SQL injections and such.

Upvotes: 1

anon
anon

Reputation:

session_start();

$_SESSION['everthing'] = $_POST['username'];

Then you can echo out the session, or just assign a variable to it. Sessions will stay there until the browser is closed

Upvotes: 0

Related Questions