How to pass multiple values of a variable through a URL

How to pass multiple values of a variable through a URL

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

Stefan Bogdanescu

Founder & Senior Architect

Handling Multi-Value Parameters in PHP URLs

When building a reporting tool where a single query parameter needs to represent multiple selections, the default behavior of standard HTML forms can be misleading. A standard <select> element usually submits only one value per name attribute. To pass multiple values like book and cd simultaneously, you must explicitly configure the form field as a list.

In your specific case, you are using a <select> element with the multiple attribute:

<select name="types" size="19" multiple>
    <option value="all" selected>All Types</option>
    <option value="book" selected>Books</option>
    <option value="cd" selected>CD</option>
</select>

While the multiple attribute enables the user to select several options, it does not automatically define how those selections are serialized into the URL. If you simply submit this form without additional attributes, PHP will likely receive only the last selected value or a single string depending on your server configuration. To ensure every selection is captured as a separate key-value pair in $_GET, you must add the multiple attribute to the <form> tag itself.

<form action="report.php" method="get">
    <select name="types[]" size="19" multiple>
        <option value="all" selected>All Types</option>
        <option value="book" selected>Books</option>
        <option value="cd" selected>CD</option>
    </select>
    <!-- Other form elements -->
    <input type="submit" value="Generate Report">
</form>

Notice the change from name="types" to name="types[]". In PHP, appending square brackets to a variable name indicates that you expect an array of values. When the form is submitted, the URL will no longer look like ?variable=value1,value2. Instead, it becomes:

localhost/report.php?types[]=book&types[]=cd

This distinction is critical for backend processing. Your PHP script must now access these values using array syntax:

// Accessing the selected types
$selectedTypes = $_GET['types']; // Returns ['book', 'cd']

// Handling the "All Types" case
if (in_array('all', $selectedTypes)) {
    // Logic for retrieving all data
} else {
    // Logic for filtering by specific types
}

If you attempt to access $_GET['types'] without checking if it is an array or without expecting multiple inputs, you risk encountering undefined index warnings or losing data. The $_GET superglobal automatically parses the URL string into a multidimensional structure when the [] notation is used in the form definition.

One common pitfall involves handling the "All Types" option alongside specific filters. If your logic expects $selectedTypes to contain only filter strings like 'book', the presence of 'all' will break a simple IN clause if you are constructing SQL queries dynamically. You must explicitly check for this sentinel value before attempting to join it with other database conditions.

$types = $_GET['types'];

if (empty($types)) {
    // Handle empty submission or default behavior
} elseif ($types === ['all']) {
    // Fetch all records
    $query = "SELECT * FROM products";
} else {
    // Convert array to comma-separated list for SQL IN clause
    $placeholders = str_replace('"', '', implode('"', $types));
    $query = "SELECT * FROM products WHERE type IN ('$placeholders')";
}

Security considerations are paramount when passing multiple values. Since the data comes directly from a <select> element, you generally do not need to sanitize inputs for injection attacks as extensively as free-text fields. However, you must ensure that the database column names or types referenced in your WHERE clause match exactly what is stored. Mismatched casing or typos in option values can silently exclude valid records from your results.

For complex applications where handling raw arrays becomes cumbersome, frameworks like Laravel provide helper methods to simplify this process. Using the request() facade or a form request class allows you to validate that the types parameter exists and contains only allowed values before executing database queries. This adds a layer of safety and clarity that is especially useful in production environments.

Ultimately, the key to passing multiple variable values lies in the combination of the multiple attribute on the select tag, the [] suffix on the form field name, and proper array handling in your backend logic.

Our network