How to Compare URL Paths and Query Parameters in a Search Form Submission

Debug search forms by comparing URL paths, repeated and encoded query parameters, and GET versus POST submissions, with a practical browser example.

Published
Reading time
7 min read
How to Compare URL Paths and Query Parameters in a Search Form Submission
On this page

A search form can send values in the URL, in the request body, or in both. To compare URL path and query parameters in a form submission, start by identifying the browser’s request method and action, then inspect the resulting request rather than assuming the fields changed the path.

Key Takeaways

  • A native GET form normally places its named, enabled controls in the query string. The action determines the destination path.
  • Repeated names can produce multiple values; a blank value is different from a missing parameter.
  • Compare parsed values as well as raw URLs: different encodings can represent the same value, while ordering or repeated values may matter to your application.
  • POST can keep search terms out of the URL, but it does not make them secret or replace HTTPS and careful logging.

Reproduce a GET search submission

Save this as index.html in an otherwise empty directory:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Search form example</title>
</head>
<body>
  <form action="/search" method="get">
    <label>
      Search
      <input type="search" name="q" value="red &amp; blue">
    </label>
    <label>
      <input type="checkbox" name="tag" value="web" checked>
      Web
    </label>
    <label>
      <input type="checkbox" name="tag" value="forms" checked>
      Forms
    </label>
    <label>
      Sort
      <input type="text" name="sort" value="">
    </label>
    <button type="submit">Search</button>
  </form>
</body>
</html>

For a local demonstration, run python3 -m http.server 8000 in that directory and open http://localhost:8000/. Submit the form, then inspect the address bar and the request in your browser’s developer tools. The static server does not implement a search endpoint, so a not-found response is expected; the request URL is the part to inspect.

With the displayed values, the destination is expected to have the path /search and a query like q=red+%26+blue&tag=web&tag=forms&sort=. The form does not place the search term in the path. red &amp; blue in the HTML is the input value red & blue; in the query, the ampersand is encoded as %26 so it is not mistaken for a parameter separator.

This example has not been executed here, and the HTML Living Standard sections on forms and URL handling could not be consulted in this run. Check those sections when verifying behavior for your target browser or a more complex form.

Advertisement

How method and action change the request

The action specifies the submission destination. An action beginning with /, such as /search, is relative to the origin’s root. A relative action such as search is resolved against the document’s base URL and can therefore lead to a different path.

For a native GET submission, the browser serializes the form controls into the destination URL’s query. In particular, do not rely on a query already in the action to supply fixed parameters: for a GET form, the serialized form data replaces that query. Put a fixed value in a named hidden control instead, such as <input type="hidden" name="source" value="home">.

Changing the example to method="post" sends the controls in the request body instead. With the default form encoding, that body is URL-encoded form data. The action still determines the destination URL, including any query already in the action. A real POST demonstration requires a server endpoint that accepts POST and reads the body; the static server above is only for inspecting the GET request.

A submit button can override the form’s action or method with formaction or formmethod. JavaScript can also intercept submission and construct a different request. Inspect the actual request when debugging a framework or custom search UI: its routing and serialization choices are not universal browser behavior.

Distinguish paths, repeated values, and blanks

For a URL shaped like /search?q=red+%26+blue&tag=web&tag=forms&sort=:

  • The path is /search. A route such as /search/books would put books in the path only because the action or application constructed that path.
  • The query begins after ?. Both tag entries are present, in their submitted order.
  • sort= is a present parameter with an empty value. It is not the same as an absent sort parameter.
  • In form-style query encoding, + represents a space, while a literal plus is encoded as %2B. Percent encoding also represents characters such as & and non-ASCII text using encoded bytes.

A control without a name, a disabled control, or an unchecked checkbox is not submitted as an ordinary field. That can explain why a parameter is missing. Server frameworks differ in how they expose repeated names: one may return a list, while another may select a single value. Check the behavior of the server code that receives the request.

Compare two URLs in the browser

The following browser-console snippet compares origins and paths, then compares decoded query values by name. It ignores the order of different parameter names but preserves the order of repeated values for each name. Replace the two example strings with the URLs you want to inspect.

const first = new URL('/search?q=red+%26+blue&tag=web&tag=forms&sort=', location.href);
const second = new URL('/search?sort=&tag=web&q=red%20%26%20blue&tag=forms', location.href);

function sameQuery(a, b) {
  const names = new Set([...a.searchParams.keys(), ...b.searchParams.keys()]);
  return [...names].every((name) => {
    const left = a.searchParams.getAll(name);
    const right = b.searchParams.getAll(name);
    return left.length === right.length &&
      left.every((value, index) => value === right[index]);
  });
}

console.log('Same origin:', first.origin === second.origin);
console.log('Same path:', first.pathname === second.pathname);
console.log('Same query values:', sameQuery(first, second));

The two example queries are expected to compare as equal under this policy: %20 and + both decode to spaces in these query values, and the two tag values retain their order. Removing sort= would make the queries differ. Reversing the two tag values would also make them differ; if your application treats tag order as irrelevant, sort those values before comparing them.

This is a debugging comparison, not a universal definition of URL equivalence. URL resolves relative references and normalizes some URL details, but the path comparison remains a comparison of the resulting serialized path strings. Your server may treat trailing slashes, encoded path characters, or case differently. The snippet ignores fragments, which browsers do not send as part of the HTTP request, and it does not compare request methods or POST bodies.

Advertisement

Privacy, caching, and choosing POST

GET search terms in a URL can appear in browser history, server and intermediary logs, analytics, shared links, and potentially referrer information. Query strings can also affect cache keys, although actual caching depends on response headers and the caches involved. For sensitive results, configure an appropriate response policy, such as Cache-Control: no-store, and review logging and analytics as well.

POST may be more appropriate when search criteria are sensitive, too large or complex for a practical URL, or should not be easily shared as a link. It trades away some convenience: a POST search is less straightforward to bookmark or share, and repeating it may require resubmission. POST bodies can still be logged or inspected by servers and intermediaries, so use HTTPS and apply access controls and data-handling policies regardless of method.

Common Mistakes

  • Comparing only the address bar when JavaScript may have sent a separate request. Check the network request’s method, URL, and body.
  • Using a single-value lookup and overlooking repeated parameters. Use getAll() when duplicates matter.
  • Treating an empty value, an absent field, and an unchecked checkbox as interchangeable.
  • Assuming query parameter order is irrelevant to every application, or that every server parses duplicates the same way.
  • Assuming a path or query comparison validates a URL for security-sensitive fetching. Server-side authorization and destination controls are separate concerns.

Best Practices

Define what equality means for your application: origin, path, parameter names, duplicate-value order, blanks, and any relevant body fields. Capture one known-good request and one problematic request, then compare each component separately. Test with spaces, literal plus signs, ampersands, repeated names, and empty controls before changing routing or form code.

Conclusion

For a native search form, inspect the action and method first: the action selects the destination, while GET puts submitted controls in the query and POST normally puts them in the body. Parse and compare values according to your application’s rules rather than relying on raw URL strings alone.

What would your domain sell for?

A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.