How to Pass Variables to the Next Page in PHP

Ralfh Bryan Perez Feb 02, 2024
  1. Use GET and POST Through HTML Form
  2. Use session and cookie
How to Pass Variables to the Next Page in PHP

A PHP variable is a symbol or name that’s equivalent to value. It is being used to store values such as values, numbers, characters or memory addresses so they can be utilized in any part of the program. A simple variable can be used in any part of the program, but it’s not accessible outside of it unless it’s being passed using a GET and POST through HTML form, session, or cookie.

Use GET and POST Through HTML Form

HTML form is one of the most powerful features of PHP. Any form element will be automatically available to the action destination of the form.

POST Request

<form action="nextPage.php" method="POST">
    <input type="text" name="email">
    <input type="text" name="username">
    <input type="submit" name="submit">
</form>

Fetching data to nextPage.php

$username = isset($_POST['username']) ? $_POST['username'] : "";
$email       = isset($_POST['email']) ? $_POST['email'] : "";
echo "Username: ".$username;
echo "Email: ".$email;

The sample output of the script may be:

Username: johndoe
Email: johndoe@gmail.com

The example above shows how to pass a variable using POST through an HTML form. The form element needs to have the action and method attribute. The action contains the next page, in this case, it’s nextPage.php. The method can be a POST or GET. You can then access the elements in the nextPage.php using a $_POST or $_GET.

GET Request

<?php
$phpVariable = "Dog";
?>
<a href="nextPage.php?data=<?=$phpVariable?>">Bring me to nextPage</a>

This example will create a GET variable and can be accessed on nextPage.php.

Example:

echo $phpVariable = $_GET['phpVariable'];
//output: Dog

The GET can be accessed using $_GET

Another way is to add a hidden element in an HTML form that submits to the next page.

Example:

<form action="nextPage.php" method="POST">
    <input type="hidden" name="phpVariable" value="Dog">
    <input type="submit" name="submit">
</form>

nextPage.php

//Using POST
$phpVariable = $_POST['phpVariable'];
//Using GET
$phpVariable = $_GET['phpVariable'];
//Using GET, POST or COOKIE;
$phpVariable = $_REQUEST['phpVariable'];

You can change the method from POST to GET to use the GET request. POST and GET are bot insecure, but GET is easier to hack since it’s available through the front-end.

The $_REQUEST can both accept GET, POST or COOKIE. It’s good to use $_REQUEST on self-referential forms for validations.

The session and cookie are easier to use, but the session is far more secure than the cookies, but not completely secure.

session

//page 1
$phpVariable = "Dog";
$_SESSION['animal'] = $phpVariable;

//page 2
$value = $_SESSION['animal'];
Note
When using the session, always remember to add session_start() on both pages before accessing the $_SESSION array.
//page 1
$phpVariable = "Dog";
$_COOKIE['animal'] = $phpVariable;

//page 2
$value = $_COOKIE['animal'];

The most obvious difference between cookies and session is that that session will be stored on the server-side while the cookies will have the client side as the storage.

Related Article - PHP Variable