How to pass variables from one php page to another without form

How to pass variables from one php page to another without form


public Coding Tips  Comment Now

[31 December 2019] public Public





If you want to pass the php variables from one page to another, especially when you have to update any rows in a table, you have to use a specific tips for that.

Usually you have to pass on the variables from one page to another page by defining the variables through URLs.

For example

www.webypost.com/status?=myvariabletodefine

You have to use the sessions for the easy way of passing variables from one page (page1.php) to other php page (page2.php)

Follow the below code to acheive this and pass on the varibles to use the same for any other queries to run on to 'echo' the same varible in the other page.

Code should be included in Page1.php

Code of page1.php

<?php
// page1.php

session_start();

echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>

Code should be included in Page2.php

<?php
// page2.php

session_start();

echo 'Welcome to page #2<br />';

echo $_SESSION['favcolor']; // green
echo $_SESSION['animal'];   // cat
echo date('Y m d H:i:s', $_SESSION['time']);

// You may want to use SID here, like we did in page1.php
echo '<br /><a href="page1.php">page 1</a>';
?>

In this way you can pass on the varible from one page to other page. 

Suggested Reading:  How to Intercept a jquery ajax call with confirm


Like & Share

Leave a Comment


Login to post a public Comment

There are no comments yet. Comment to discuss