Home  >  Article  >  Backend Development  >  How can I pass multiple variables to another page in a URL using the ampersand (&)?

How can I pass multiple variables to another page in a URL using the ampersand (&)?

Linda Hamilton
Linda HamiltonOriginal
2024-10-26 12:37:29992browse

How can I pass multiple variables to another page in a URL using the ampersand (&)?

Passing Multiple Variables to Another Page in URL

In the pursuit of sharing multiple variables across pages via URL parameters, the use of sessions alone may seem restrictive. However, by incorporating the ampersand (&) character, this limitation can be overcome.

Solution:

Let's revisit the code in the question:

Page 1:

<code class="php">session_start();
$event_id = $_SESSION['event_id'];
echo $event_id;

$url = "http://localhost/main.php?email=" . $email_address . "&amp;event_id=" . $event_id;
//                                              ^ add ampersand here</code>

Page 2:

<code class="php">if (isset($_GET['event_id'])) {
    $event_id = $_GET['event_id'];
}
echo $event_id;</code>

By adding the ampersand between the concatenated variables, we effectively glue them together in the URL. This ensures that both variables can be retrieved on Page 2:

<code class="php">$event_id = $_GET['event_id']; // successfully retrieved</code>

In this manner, you can effectively pass multiple variables in a URL, enabling seamless data exchange between pages.

The above is the detailed content of How can I pass multiple variables to another page in a URL using the ampersand (&)?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn