How to access the form's 'name' variable from PHP

How to access the form's 'name' variable from PHP

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

Stefan Bogdanescu

Founder & Senior Architect

How to Access the Form's 'name' Variable from PHP

When building a BMI calculator that supports both metric and imperial inputs, you quickly run into a common architectural decision: how do you distinguish between submissions? In your specific case, you have two forms sharing identical field names (weight and height) but differing units. You've already identified that hidden fields like bmiSubmitted handle the logic for which data to process, but you are stuck on verifying which form was actually used via its HTML name attribute.

The short answer is that you generally do not need to read the form's name attribute in PHP. The browser does not send the value of a form's name property as part of the POST payload when the form is submitted. It only sends the values of inputs, buttons, and textareas.

However, relying on hidden fields like your bmiSubmitted variable is the standard pattern for this exact scenario. Your current approach using $_POST['bmiSubmitted'] to flag a submission is technically sound because it decouples the UI structure from the processing logic. If you attempt to access $_POST['met'] or $_POST['imp'], those variables will not exist, as the browser ignores the form element's name attribute during submission.

If you require a way to identify the source form without adding hidden fields, you must change how the data is submitted. The most robust solution is to include an explicit input field that acts as the identifier. You can add a hidden input named formType inside the metric form with a value of 'metric', and do the same for the imperial form. This shifts the responsibility from the element definition to the request payload, making it accessible via $_POST['formType'].

While this might feel like over-engineering compared to simply reading the form name, it ensures reliability across different browsers and server configurations. Some older or non-standard parsers might ignore the name attribute of the parent <form> tag entirely if they only parse key-value pairs from the body. By explicitly sending a value in the data stream, you guarantee that your PHP script receives the context needed to decide whether to divide by kilograms or convert inches to centimeters before calculating BMI.

Your existing code snippet demonstrates a functional implementation using the hidden flag approach. The logic inside the if (isset($bmiSubmitted)) block correctly isolates the calculation from the form selection UI. When validating user input, always ensure that the units associated with the numbers match the expected format. For instance, if formType is set to 'imperial', your code must handle unit conversion or validation errors specific to pounds and inches before applying the standard BMI formula.

For developers looking to manage complex form states without cluttering their HTML with multiple hidden fields per input group, consider frameworks that abstract this layer. Laravel provides a robust request object that simplifies accessing POST data, making it easier to validate and transform inputs dynamically based on the source context.

Ultimately, while the name attribute of a form is useful for accessibility scripts or client-side validation, server-side logic should rely on explicit data submission. If you need to identify the form that triggered a request, include an identifier within the form's input fields rather than attempting to read the element definition from the $_POST array.

Our network