Viewed   1.9k times

It seems pretty simple but I can't find a good way to do it.

Say in the first page I create a variable

$myVariable = "Some text";

And the form's action for that page is "Page2.php". So in Page2.php, how can I have access to that variable? I know I can do it with sessions but I think it's too much for a simple string, and I do only need to pass a simple string (a file name).

How can I achieve this?




HTML / HTTP is stateless, in other words, what you did / saw on the previous page, is completely unconnected with the current page. Except if you use something like sessions, cookies or GET / POST variables. Sessions and cookies are quite easy to use, with session being by far more secure than cookies. More secure, but not completely secure.


//On page 1
$_SESSION['varname'] = $var_value;

//On page 2
$var_value = $_SESSION['varname'];

Remember to run the session_start(); statement on both these pages before you try to access the $_SESSION array, and also before any output is sent to the browser.


//One page 1
$_COOKIE['varname'] = $var_value;

//On page 2
$var_value = $_COOKIE['varname'];

The big difference between sessions and cookies is that the value of the variable will be stored on the server if you're using sessions, and on the client if you're using cookies. I can't think of any good reason to use cookies instead of sessions, except if you want data to persist between sessions, but even then it's perhaps better to store it in a DB, and retrieve it based on a username or id.


You can add the variable in the link to the next page:

<a href="page2.php?varname=<?php echo $var_value ?>">Page2</a>

This will create a GET variable.

Another way is to include a hidden field in a form that submits to page two:

<form method="get" action="page2.php">
    <input type="hidden" name="varname" value="var_value">
    <input type="submit">

And then on page two:

//Using GET
$var_value = $_GET['varname'];

//Using POST
$var_value = $_POST['varname'];

//Using GET, POST or COOKIE.
$var_value = $_REQUEST['varname'];

Just change the method for the form to post if you want to do it via post. Both are equally insecure, although GET is easier to hack.

The fact that each new request is, except for session data, a totally new instance of the script caught me when I first started coding in PHP. Once you get used to it, it's quite simple though.

Monday, December 12, 2022

You can do this using session

1) In first page, after submitting first form store the email value in session like below

<?php session_start();
$_SESSION['email'] = $_POST['email'];

2) In second page, you have to start the session and access the session variable like this

<?php session_start();

$email = $_SESSION['email']; //from previous login page
// And remaining code here ....
Tuesday, August 2, 2022

Use session variables.

First of all every page you want to use sessions on needs to have the function session_start(); declared at the top of every page. You then can use the session superglobal to store things.

    $_SESSION['variable_name'] = 'string';

    echo $_SESSION['variable_name'];
Pg2.php will show: some string
Saturday, August 27, 2022

Use string interpolation with double quotes for the echo statement and single quotes everywhere inside the javascript:

echo "<script type='text/javascript' language='javascript'>'$file'); 

The interpolated PHP variable $file should be correctly interpreted as a string and the value it holds should be displayed in the URI of your javascript. Check out this easy to understand info about variable interpolation

Saturday, September 17, 2022

You need to edit checkbox code as :

<td><input type="checkbox" name="checkbox[]" value="<?php echo $res['train_id']?>"></td>

customer_list.php code:


function getAllAssoc_id($id) {
        $sql = "SELECT * FROM train_information WHERE id = :id";
        $sth = $this->pdo->prepare($sql);
        $sth->bindParam(":id", $id);
        return $sth->fetchAll();

    //echo $_POST["checkbox"];

    echo"<table><tr><td>Train Name</td><td>Train Id</td></tr>";
    foreach($_POST["checkbox"] as $key=>$val){

Wednesday, October 12, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :