Part 2: Pass values and Call each others (PHP, HTML, Javascript)

Continue from Passing value from one to another considering PHP, HTML and JavaScript

Pass PHP variable from one page to another (in PHP)
(Get data from one page and send to another page in php)

1. If you are getting data from user through form (<form>), all input data will be automatically available to the page you mention in action (<form action='targetpage.php'>).
Your all data will be in $_POST or $_GET array depending on what you mentioned in method of the form (method=”post”).
2. If you want to share data among many pages then you can use $_SESSION, $_COOKIE, file or table. The data can be database table (mysql) retrieved data or user given data. For example:- you may like to show user name on every page.

Using session –


<?php
session_start();
--
--
$_SESSION[‘userName’] = ‘Dear PHP MySQL’;

On next page/another page, retrieve data like this:


<?php
session_start();
…
..
echo $_SESSION['userName'];
// unset ($_SESSION['userName']); // delete if not required further

You will get the session value on the same page after reload.

Using cookie –

Setcookie (‘userName’, ‘Dear PHP and MySQL’);

On another page or on same page on revisit
Echo $_COOKIE[‘userName’];
// setcookie(‘userName’, ‘’); // delete if not used further

Using file for passing data in various pages:


$str = ‘Dear PHP MySQL’;
$hn = fopen (‘filepath/fileName.php’, ‘w+’);
if ($hn) {
fwrite($hn, $str);
fclose ($hn);
}


On any page:


// get file data
$hn = fopen (‘filepath/fileName.php’, ‘r’);
if ($hn) {
$str = fread($hn, filesize($filename));
echo $str;
fclose ($hn);
}

You will use "global" for getting variable value in function, if the variable is defined outside of the function.

If you want to get and share data in another session or between different users then you will need to use file read/write method or you have to use table as PHP don’t have application variable as .NET have.