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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Pass Variables Between PHP Pages Without Forms

When a user clicks a link on your site and navigates to another page, you often need to carry data along that journey. The most obvious method involves wrapping the link in an HTML form and submitting it via GET or POST. However, this approach feels clunky: you are forcing a full page reload just to transfer a simple string, which can disrupt user flow and increase latency.

If your goal is simply to pass a variable containing a string from one page to another for use in database queries, there are lighter-weight mechanisms available. The standard solution involves using URL query parameters. This technique leverages the GET request method, passing data as key-value pairs appended to the URL.

To implement this, you construct the hyperlink by embedding your variable directly into the href attribute. If your string is stored in a PHP variable named $searchTerm, the link tag would look like this:

<a href="results.php?query=<?php echo urlencode($searchTerm); ?>">View Results</a>

The critical detail here is the use of urlencode(). Since strings can contain spaces, special characters like ampersands, or even quotes, passing them raw into a URL breaks the parsing logic. The query string parser expects key-value pairs separated by & and values separated from keys by =. Without encoding, a value like "Hello World" becomes query=Hello%20World. If you skip encoding, the space might be misinterpreted as a separator, causing the entire parameter to fail or truncate.

On the destination page, say results.php, you access this data via the superglobal array $_GET. The code to retrieve and sanitize the input for a MySQL query looks like this:

$queryString = $_GET['query'] ?? '';
if (!empty($queryString)) {
    // Proceed with database logic
}

The null coalescing operator (??) ensures that if the parameter is missing, you don't introduce empty strings or undefined variable errors. You must remember to sanitize this input before inserting it into a SQL query to prevent injection attacks, regardless of how simple the data source seems.

While URL parameters are the standard for GET requests, they have limitations. They are visible in the browser history and can be bookmarked, which is desirable for sharing links but problematic if the string contains sensitive information or tokens. If you need to pass a variable that shouldn't appear in the URL, server-side session management is the alternative.

Sessions store data on the server rather than the client. You initialize a session with session_start() at the top of both pages. To set a value, use $_SESSION['variableName'] = $value;. On the second page, you retrieve it from $_SESSION['variableName']. This method hides the variable from the URL entirely and allows for more complex data structures, though it requires maintaining state across requests.

Choosing between URL parameters and sessions depends on the nature of the data and the desired user experience. For search terms, filter values, or ID references, GET parameters are transparent and intuitive. For temporary data that should not be exposed or bookmarked, sessions provide a cleaner separation. Both methods avoid the overhead of submitting an entire form, keeping your navigation smooth and efficient.

Our network