Get selected text from dropdown in PHP

Get selected text from dropdown in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Extracting Display Text from a Dropdown in PHP

When building forms, developers often focus on capturing the submitted data correctly. In your specific case, you are using a standard HTML <select> element where the name attribute is set to cscf[country]. When the form is submitted via POST or GET, PHP populates the $cscf['country'] array with the value of the selected option's value attribute. In your example, that value is an email address like malaysia@email.com.

While this works for database storage, it creates a problem if you need to display the result back to the user or pass it to another system expecting the human-readable country name. You cannot simply rely on the submitted value because it lacks the semantic meaning of "Malaysia." To retrieve the display text, you must access the corresponding option element's text content directly within your PHP logic.

The most straightforward approach is to iterate through the HTML string or use DOM manipulation if the dropdown is generated dynamically. However, given that you are working within a WordPress plugin context, relying on raw HTML parsing can be fragile. A more robust solution involves maintaining a mapping between the stored value and the display text in your application logic.

If you prefer sticking to server-side processing without JavaScript, you can parse the submitted data against a predefined list. For instance, create an associative array where keys are the email values used in the <option> tags and values are the country names:

$countryMap = [
    'malaysia@email.com' => 'Malaysia',
    'indonesia@email.com' => 'Indonesia',
];

// Retrieve the selected value from the form submission
$selectedValue = isset($_POST['cscf']['country']) ? $_POST['cscf']['country'] : '';

// Map the value to the display text
$displayCountry = $countryMap[$selectedValue] ?? 'Unknown';

This pattern is common in legacy systems but requires strict consistency between your database values and your lookup array. If a new country is added to the dropdown, you must remember to update this mapping immediately; otherwise, you will receive an empty string or a fallback value. This introduces a maintenance overhead that often leads to data inconsistency over time.

A more modern and scalable approach, particularly within the WordPress ecosystem, is to leverage Laravel's Eloquent ORM or a similar database model if your plugin interacts with a central database. If your country list is stored in a database table rather than hardcoded HTML, you can fetch the selected value from the request and query the database for the corresponding label:

$countryCode = $_POST['cscf']['country'];

// Assuming you have a Country model
$country = \App\Models\Country::where('email', $countryCode)->first();

if ($country) {
    $displayText = $country->name;
} else {
    $displayText = 'Unknown';
}

This method decouples your display logic from the HTML structure. It ensures that even if the order of options in the dropdown changes, or if you decide to use a different value identifier (like ISO codes), your application will still resolve the correct name.

If you are working with Laravel specifically and want to streamline this process, consider defining an enum or a static configuration file for country mappings. This allows you to keep your form rendering logic separate from your data resolution logic. For example, you could use a helper function that accepts the raw value and returns the localized name, ensuring consistency across your entire plugin.

While parsing the HTML of the submitted form using DOMDocument is technically possible to extract text by ID, it is generally discouraged in production environments unless you are dealing with legacy systems where no other data exists. It breaks encapsulation and makes testing difficult because your output depends on the exact HTML structure of a specific request rather than stable business logic.

Ultimately, the choice between a simple lookup array and a database query depends on how dynamic your country list needs to be. If the list is static and small, an associative array offers zero-latency resolution. If the list grows or requires frequent updates, a database-backed model provides better long-term maintainability.

Our network