PHP: HTML: send HTML select option attribute in POST

PHP: HTML: send HTML select option attribute in POST

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

Stefan Bogdanescu

Founder & Senior Architect

Handling Custom Attributes in Form Submissions with PHP

The example code you provided demonstrates a common misconception about how form data is transmitted over HTTP. In your HTML snippet, you have defined an option element with both a value attribute and a custom attribute named stud_name:

<select name="age">
    <option value="1" stud_name="sre">23</option>
    <option value="2" stud_name="sam">24</option>
    <option value="5" stud_name="john">25</option>
</select>

When this form is submitted via a POST request, PHP's $_POST superglobal will only contain the data explicitly defined in the value attribute of the selected <option> tag. In the specific case above, if the user selects the first option, $_POST['age'] will be "1". The value "sre" associated with the stud_name attribute is effectively invisible to the server during standard form submission.

This behavior exists because the HTML specification strictly defines what constitutes "input data" for forms: it is limited to the value attribute of inputs, selects, and textareas. Custom attributes like data-* or arbitrary names like stud_name are treated purely as metadata for the client-side document object model (DOM) and do not contribute to the form's payload unless explicitly handled differently.

The Standard Approach: Embedding Data in Value

If your goal is to capture both the numeric age and the student name in your PHP handler, the most robust and standards-compliant solution is to encode the necessary information directly into the value attribute of the option. This ensures that a single POST variable contains all required data without relying on client-side JavaScript or server-side hacks.

You can achieve this by concatenating the values within the HTML:

<select name="age_data">
    <option value="1:sre">23</option>
    <option value="2:sam">24</option>
    <option value="5:john">25</option>
</select>

In your PHP controller, you would then parse this string. While simple enough for small datasets, this approach introduces parsing logic in your backend. You must decide on a delimiter that cannot appear in the data (like a colon : or pipe |) and implement validation to ensure the format matches expectations before splitting the string.

The Modern Approach: Using Data Attributes

A cleaner separation of concerns involves using the data-* attribute convention. While stud_name is not a standard form field, you can add it as a data attribute on the <option> tag. However, HTML forms cannot natively send data-* attributes via POST because they are ignored by the browser's form submission logic.

To capture these values, you must leverage JavaScript to intercept the form submission or read the DOM state before the request is sent. This shifts some logic to the client side but keeps your HTML semantic and clean.

<form id="addForm" name="add">
    <label>Age: </label>
    <select name="age" data-student-map='{"1":"sre","2":"sam","5":"john"}'>
        <option value="1">23</option>
        <option value="2">24</option>
        <option value="5">25</option>
    </select>

    <input type="submit" name="submit" value="Submit">
</form>

<script>
document.getElementById('addForm').addEventListener('submit', function(e) {
    e.preventDefault(); // Prevent immediate submission
    
    const selectedOption = this.querySelector('select option:checked');
    const ageValue = selectedOption.value;
    
    // Retrieve the mapped name from a data attribute on the select or a JS object
    const studMap = JSON.parse(this.querySelector('select').getAttribute('data-student-map'));
    const studentName = studMap[ageValue];
    
    // Construct the payload
    const formData = new FormData();
    formData.append('age', ageValue);
    formData.append('stud_name', studentName);
    
    // Send via AJAX or replace submit behavior
    fetch('/submit-handler', {
        method: 'POST',
        body: formData
    });

    this.submit(); // Proceed with standard POST if needed
});
</script>

This pattern allows you to maintain a clear mapping between the selected value and the custom attribute without cluttering your value attributes. It also prevents issues where special characters in names might break simple string parsing logic.

Laravel Specific Considerations

If you are building this application using the Laravel framework, handling form data remains fundamentally the same regarding browser limitations; however, Laravel provides tools to make the backend processing safer and more expressive.

When receiving a request that includes stud_name (either via the concatenated value method or AJAX), you should always access it via the request() helper rather than directly accessing $_POST. This facilitates better type handling and validation.

// Assuming 'age_data' is sent as "1:sre"
$ageValue = $request->input('age_data');
list($numericAge, $studentName) = explode(':', $ageValue);

// Or if using AJAX to send separate fields
$request->validate([
    'age' => 'required|integer',
    'stud_name' => 'required|string|max:255'
]);

$student = new Student();
$student->age = $request->input('age');
$student->name = $request->input('stud_name');

For more complex scenarios involving rich form interactions, Laravel's Form Request classes offer a structured way to validate these payloads before they reach your business logic. Additionally, if you are looking for comprehensive guides on handling form requests in modern PHP stacks, resources like https://laravelcompany.com often provide up-to-date patterns that align with current Laravel best practices.

The fundamental rule remains: standard HTML forms only transmit value attributes. Any additional data intended for the server must either be encoded into that value or transmitted through an alternative mechanism like JavaScript-driven AJAX requests.

Our network