user1565309
user1565309

Reputation: 41

maintain session between the pages in php?

I am very new to PHP.

I want to transfer data between pages.

In my requirements I have the home page first, in that I have 3 fields: name, address, pin and then submit button.

When I entered the above fields and then click on submit, it moves to page2.php, it has form data.

I have transferred the first form data to second page. Now in second page I have a submit button. When I click on that button the data is submitted to a MySQL database.

My problem is, how can I move first page values to insertdata.php page and submit the data ?

Upvotes: 4

Views: 33867

Answers (5)

asprin
asprin

Reputation: 9823

There are two ways with which you can do this

  1. Sessions
  2. Hidden input fields

Sessions

To pass data from one page to another, you first need to call session_start() on all pages that are going to use $_SESSION superglobal variable. Then you can store your values in sessions by using

$_SESSION['name'] = $_POST['name'];
$_SESSION['address'] = $_POST['address'];
$_SESSION['pin'] = $_POST['pin'];

To use these values in the second page, simply call them by their names. Ex:

$name = $_SESSION['name']; // will contain the value entered in first page

==================================================================================

Hidden Input Fields

This is more of a tedious approach but it does the job none the less. The process involves storing data that needs to be passed onto to different pages in hidden fields and later accessing them via the $_POST or $_GET superglobal.

page1.php (which posts to page2.php)

<input type="text" value="Page 1 content" name="content" />
<input type="text" value="Page 1 body" name="body" />

page2.php (which posts to page3.php)

<input type="hidden" value="<?php echo $_POST['content']; ?>" name="content" />
<input type="hidden" value="<?php echo $_POST['body']; ?>" name="body" />
<input type="text" value="Page 2 content" name="content2" />
<input type="text" value="Page 2 body" name="body2" />

page3.php

echo $_POST['content']; // prints "Page 1 content"
echo $_POST['body']; // prints "Page 1 body"
echo $_POST['content2']; // prints "Page 2 content"
echo $_POST['body2']; // prints "Page 2 body"

Upvotes: 11

Varun Sridharan
Varun Sridharan

Reputation: 1978

just use the below code in your first page <?php session_start(); ?> and use the following code in your sencond page

<?php
$name = $_SESSION['name'];
$address = $_SESSION['address'];
$pin = $_SESSION['pin'];
echo $name."<br/>";
echo $address."<br/>";
echo $pin."<br/>";
?>

or you can use post or get method as below

For GET Method

<?php
$name = $_GET['name'];
$address = $_GET['address'];
$pin = $_GET['pin'];
echo $name."<br/>";
echo $address."<br/>";
echo $pin."<br/>";
?>

For POST METHOD

    <?php
    $name = $_POST['name'];
    $address = $_POST['address'];
    $pin = $_POST['pin'];
    echo $name."<br/>";
    echo $address."<br/>";
    echo $pin."<br/>";
    ?>

Upvotes: 1

sundar
sundar

Reputation: 1760

Like REQUEST, PHP maintains a map for session in which you could dump these values and across the pages. e.g.

$_SESSION['name']='foo';

But the page where you would like to use session, you need to start the session using the method session_start(); . But this method call should be the first line in your php.

http://www.w3schools.com/php/php_sessions.asp

Upvotes: 0

thedethfox
thedethfox

Reputation: 1741

Basically when you submit the data in the first page save them in the session then redirect to the second page.

In the second page just load them.

Page 1:

<?php session_start(); 

     $_SESSION["var_name"]  = $some_value;


?>

page 2:

<?php session_start(); 

     $my_var = $isset($_SESSION["var_name"])?$_SESSION["var_name"]:null;




?>

you test the value of my_var, if it's empty then redirect to the first page or show an error message.

session tutorial http://www.w3schools.com/php/php_sessions.asp

Mysql insert tutorial http://www.tizag.com/mysqlTutorial/mysqlinsert.php

POST/GET tutorial http://www.tizag.com/phpT/postget.php

Upvotes: 0

Sinmok
Sinmok

Reputation: 656

To store data between page changes, you can use the superglobal $_SESSION array.

Example

Page 1

$_SESSION['name'] = "John";

Page 2

echo $_SESSION['name'];

Output

John

Make sure you put session_start() at the top of each page that requires the use of sessions.

Click here for more information on Sessions.

Upvotes: 0

Related Questions