posting hidden value

posting hidden value

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

Stefan Bogdanescu

Founder & Senior Architect

Why Hidden Inputs Don't Work for Cross-Page Data Transfer

The core issue in your current setup stems from a fundamental misunderstanding of how HTML forms and server-side execution flow interact. You are attempting to pass data from bookingfacilities.php to successfulbooking.php, but the code snippet you provided reveals why this approach fails: you are trying to echo PHP variables directly into the value attribute of an input field using a mix of single and double quotes that prevents proper variable interpolation, or worse, you might be echoing them at a stage where they haven't been set yet.

In your specific example from successfulbooking.php, the line <input type="hidden" name="date" id="hiddenField" value="<?php print "$date" ?>"/> contains a syntax error regarding quote matching and variable scope. If $date is defined in bookingfacilities.php, it does not exist when successfulbooking.php runs. PHP variables are local to their execution context unless explicitly passed along. An HTML file cannot magically know the value of a PHP variable defined three files ago just by being on the same server.

To solve this, you must leverage the standard mechanism designed for exactly this purpose: form submissions via HTTP requests. The most robust way to pass data from one page to another in a multi-step booking flow is using the GET or POST method with query parameters or hidden fields within a form tag that explicitly submits to the destination URL.

Using Query Parameters (GET)

If your data is non-sensitive and you want to keep the state visible in the browser's address bar, append the values as query parameters to the URL linking the pages. In bookingfacilities.php, after processing the user's input, construct a redirect that includes the $date variable:

// bookingfacilities.php logic
$date = "$day-$month-$year";

// Redirect with data attached
header("Location: booking_now.php?date=" . urlencode($date));
exit;

When the user clicks the button to proceed, they are sent to booking_now.php with ?date=2023-10-25 in the URL. In booking_now.php, you can read this directly from $_GET['date'] and then submit a form that posts this data (or simply passes it further) to successfulbooking.php.

// booking_now.php logic
$receivedDate = $_GET['date'] ?? null;

if ($receivedDate) {
    // Pass to next step via POST or another redirect
    header("Location: successfulbooking.php?date=" . urlencode($receivedDate));
}
exit;

Using Hidden Fields (POST)

If you prefer the data to be hidden from the URL, use a hidden input field within a form that submits to the next page. However, the critical detail is ensuring the value is set before the form is rendered or submitted.

In bookingfacilities.php, create a form that submits to booking_now.php with the date already populated in a hidden field:

// bookingfacilities.php
$date = "$day-$month-$year";

echo <<<HTML
<form action="booking_now.php" method="POST">
    <input type="hidden" name="date" value="<?php echo htmlspecialchars($date); ?>">
    <!-- Other form fields -->
    <button type="submit">Proceed</button>
</form>
HTML;

In booking_now.php, you receive the data via $_POST['date'] and then initiate a redirect to successfulbooking.php. This ensures the data travels through the HTTP request/response cycle rather than relying on static HTML that never executes server-side code.

Why Your Original Approach Failed

Your original attempt in successfulbooking.php tried to define the value directly in the input tag using <?php print "$date" ?>. Even if the syntax were correct, this would only work if $date was defined within successfulbooking.php. Since PHP does not maintain state between separate page loads without passing it via HTTP requests or sessions, referencing a variable from a previous file is impossible.

Additionally, mixing single quotes inside double quotes (and vice versa) in your string concatenation can lead to unexpected behavior where the parser stops reading the string early, preventing the PHP code inside from ever executing. Always ensure your quotes match and use htmlspecialchars() when outputting user-controlled data to prevent XSS vulnerabilities.

When to Use Sessions

If you need to preserve complex state across multiple intermediate pages (like booking_now.php), passing everything as URL parameters can become messy. In such cases, use PHP sessions ($_SESSION). Store the date in $_SESSION['date'] on the first page and retrieve it on the final page. This approach is cleaner for multi-step wizards but requires ensuring the session is started with session_start() at the top of every script involved.

For a project due tomorrow, sticking to the redirect pattern with GET parameters or hidden forms with POST is the quickest fix. It aligns with standard web architecture and avoids the complexity of trying to share memory between isolated HTTP requests. If you are building a larger application later, consider migrating to a framework like Laravel, which handles these state management concerns out of the box.

Our network