Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

How to Get an ID from a URL and Post It to Another PHP Page

Read URL IDs with PHP’s $_GET, carry them between pages with a named POST field, then validate the value and check authorization before changing a record.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In PHP, read an ID in a URL such as edit.php?id=25 with $_GET['id']. To carry it with a form submission, add a named hidden field and read it on the receiving page with $_POST['id']. The field’s name determines the PHP key. In either case, treat the value as untrusted input: validate its format, confirm the record exists, and check that the current user is allowed to perform the requested action.

Get an ID from a URL in PHP

For a URL like edit.php?id=25, PHP makes the query-string value available as $_GET['id']. The PHP manual explains how external variables map to request keys in variables from external sources.

As an Amazon Associate I earn from qualifying purchases.

<?php
$id = $_GET['id'] ?? null;
?>

This reads the value but does not establish that it is valid. Validate it according to the ID format your application uses before relying on it.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Post an ID to another PHP page with a form

Use a form with method="post" and an input whose name matches the key you want to read in PHP. For example, name="id" becomes $_POST['id']; the HTML id attribute is used for labels, scripts, and styling, not as the submitted PHP key. See PHP’s documentation on form variables.

<form action="update.php" method="post">
    <input type="hidden" name="id" value="25">
    <label for="title">Title</label>
    <input id="title" name="title" type="text">
    <button type="submit">Save</button>
</form>

On the receiving page, read the submitted form value from $_POST:

<?php
$id = $_POST['id'] ?? null;
?>

POST is the appropriate method when submitting a form that changes server state; PHP’s forms tutorial explains the choice in dealing with forms. GET is generally suited to a read or selection represented in the URL. Neither method validates an ID or proves that a user has permission.

Validate the ID on both pages

Request values are controlled by the requester. Validate them on the server, including on the receiving page, rather than relying on a hidden input or browser-side checks. filter_input() can apply a validation filter; with FILTER_VALIDATE_INT, it is suitable only when the application expects an integer. The function returns null when the value is absent and false when validation fails. Its default filter, FILTER_DEFAULT, performs no filtering. Details are in the PHP documentation for filter_input().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For an integer ID arriving in the URL, a page can validate it before displaying the form and escape it for the HTML attribute context:

<?php
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if ($id === false || $id === null) {
    http_response_code(400);
    exit('Missing or invalid ID.');
}
?>
<form action="update.php" method="post">
    <input type="hidden" name="id" value="<?= htmlspecialchars((string) $id, ENT_QUOTES, 'UTF-8') ?>">
    <label for="title">Title</label>
    <input id="title" name="title" type="text">
    <button type="submit">Save</button>
</form>

Then validate again in update.php, because the submitted value can be changed after the form is rendered:

<?php
$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
if ($id === false || $id === null) {
    http_response_code(400);
    exit('Missing or invalid ID.');
}

// Fetch the record using a parameterized query.
// Check that the current user may edit it before making a change.
?>

If your IDs are UUIDs or another format, use validation appropriate to that format instead of assuming a positive integer. HTML escaping prevents the value from breaking out of its attribute context; it does not validate the value or authorize an operation.

Check the record and permission before changing it

A well-formed ID may still refer to no record, or to a record the current user must not change. After validating the format, load the record with a parameterized database query, confirm it exists, and check authorization against the current user before an update, deletion, upload, or other protected action. A hidden field only carries the value; it is neither secret nor a permission check. PHP’s FAQ includes examples of carrying values in forms in PHP and HTML.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose GET or POST for the right kind of request

Question GET POST
Typical PHP access $_GET['id'] $_POST['id'] for URL-encoded or multipart form data
Where the value is carried Query string in the URL Request body
Common fit Read or select a record Submit an action that changes server state
What the method does not guarantee Validity or permission Validity or permission; POST is not access control

PHP documents query-string and form-variable handling in variables from external sources and $_POST.

When an ID does not arrive as expected

  • The submitted key is missing: Check that the input has name="id". An id="id" attribute alone does not submit a PHP field named id.
  • The value is in the wrong superglobal: A query parameter is read from $_GET; a standard form submitted with POST is read from $_POST.
  • JSON is not available in $_POST: PHP’s $_POST covers URL-encoded and multipart form bodies. For JSON or XML request bodies, PHP documents reading php://input; see $_POST.
  • Validation accepts unexpected values: Specify an intentional validation filter. FILTER_DEFAULT does not validate the ID format.
  • A valid-looking value still fails: Check whether the record exists and whether the current user is authorized to act on it; format validation alone cannot answer either question.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.