passing hidden input value by using php

passing hidden input value by using php

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

Stefan Bogdanescu

Founder & Senior Architect

The Timing Issue: Why Your Hidden Input Fails

The core problem in your snippet isn't a misunderstanding of HTTP methods or the need to switch to $_GET. It is a fundamental misunderstanding of execution order. In PHP, code executes sequentially as the script is parsed and run. When you place logic inside a form that relies on data from that same form submission, you are fighting against the timeline of a single request.

In your specific example, the line $clicked = "You have clicked the button"; runs during the initial page load when $_POST is empty. At that exact moment, the variable $clicked retains whatever value it had previously (or defaults to null/false). The form then renders with this initial value. Only after the user clicks submit and the server processes the request does PHP check if ($_POST). By then, it is too late; the hidden input has already been generated with the old value.

To fix this without introducing complex session logic or AJAX, you must separate the data source from the form generation. The standard pattern involves moving the state management to a dedicated handler script rather than trying to handle the "click" event within the form's own rendering loop.

Separating State Logic from Form Rendering

The most robust solution is to load the current state of $clicked into the view before generating the form HTML. This ensures that whatever value exists in $_POST (if any) is captured and immediately reflected in the hidden input field.

You should move the logic that sets $clicked outside of the conditional block that generates the form, or ensure it reads from $_POST before echoing it to the view. Here is a corrected approach:

// Load existing state from POST if available
$clicked = isset($_POST['HDN_FormClicked']) ? $_POST['HDN_FormClicked'] : null;

// Now generate the form with the current value of $clicked
<form method="post">
    <input type="hidden" name="HDN_FormClicked" value="<?php echo htmlspecialchars($clicked); ?>">
    <button type="submit" class="button">Submit</button>
</form>

// Process the submission AFTER the form has been rendered
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Logic to handle the click goes here
    $clicked = "You have clicked the button";
    
    // Optional: Redirect or store data to prevent re-processing (CSRF protection best practice)
    header('Location: success.php'); 
    exit;
}

In this pattern, $clicked is populated from $_POST first. If the form was just submitted, that value is already there. The hidden input captures it correctly. Then, the processing logic runs after the HTML output, ensuring the new state isn't accidentally used for the current request's form generation.

Why You Don't Need $_GET

The suggestion to use $_GET is often a misconception about how forms work. Using $_GET implies a query string parameter (e.g., ?clicked=1). While technically possible, this changes the semantics of your application:

  1. Visibility: Hidden inputs are meant for internal state management that isn't user-facing. Query strings are visible in the URL bar and browser history.
  2. Security: Exposing internal flags like "form clicked" via the URL can lead to information leakage or unintended side effects if a user manually appends parameters.
  3. Semantics: If your application relies on a POST request to signify an action, switching to GET breaks the standard HTTP verb convention, where GET is for safe, idempotent retrieval and POST is for state-changing actions.

Stick with $_POST for hidden inputs unless you have a specific reason to expose that data publicly. The issue was always about when the variable was assigned relative to when it was echoed, not which superglobal array you were using.

Handling Multiple Submissions

A common pitfall with this approach is allowing multiple clicks on the submit button. If you simply set $clicked in the if ($_POST) block without redirecting or returning early, a user might refresh the page and see "You have clicked the button" twice because the form resubmits on refresh.

To mitigate this, consider implementing a redirect after processing the POST data. This follows the Post-Redirect-Get (PRG) pattern:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $clicked = "You have clicked the button";
    
    // Store result in session or database here
    
    // Redirect to avoid form resubmission on refresh
    header('Location: /thank-you'); 
    exit;
}

// The form renders only if it's not a POST request that has already been handled

By redirecting, you ensure the hidden input reflects the action once, and subsequent requests treat the page as a standard GET request, clearing $_POST and resetting the state for the next interaction.

Our network