HTML/PHP - default input value

HTML/PHP - default input value

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

Stefan Bogdanescu

Founder & Senior Architect

Handling Dynamic Default Values in HTML Forms

When building a post-editing form, it is common to need to display existing data or default values within input fields before the user makes changes. In your specific scenario, you are dynamically generating the value attribute based on database options using PHP. This approach works perfectly when that data exists, but it breaks when you attempt to set a fallback default like "Mike" for a field where no option is stored.

The core issue here is that HTML does not support conditional logic directly within attributes. You cannot write something like value="Mike if empty, else get_option(...)" inside the value tag. The browser simply renders whatever string is present between the quotes at parse time. If your PHP script outputs an empty string when get_option() returns null, the HTML becomes <input type="text" name="your_name" value="" />. This technically satisfies the requirement for an empty default, but if you need a specific fallback text to appear, you must resolve that logic on the server side before the HTML is sent to the browser.

To force "Mike" to appear when get_option('your_name') returns nothing, you need to perform the check within your PHP logic that generates the form. The cleanest solution involves using a ternary operator or an if statement to determine which value string should be injected into the HTML.

Here is how you can structure the output block:

<?php
$name_value = get_option('your_name');

// If get_option returns null, false, or an empty string, use the fallback.
$display_value = !empty($name_value) ? $name_value : 'Mike';
?>
<input type="text" name="your_name" value="<?php echo esc_attr($display_value); ?>" />

This pattern ensures that if the database has no value for your_name, the user sees "Mike". If a value exists, it is preserved. It is crucial to wrap the output in esc_attr() (or escape the variable before assigning it) to prevent Cross-Site Scripting (XSS) vulnerabilities, especially since user input or dynamic content might eventually populate these fields.

However, relying on server-side logic for every single field creates repetition and increases the risk of inconsistency across your codebase. If you have three inputs—Name, Last Name, and My Name—as listed in your example, manually writing this conditional block for each one is error-prone. You might forget to escape a variable in one field or miss a null check in another.

A more scalable architecture involves separating the data retrieval from the view rendering. Instead of scattering logic inside every input tag, you should define defaults in a configuration array or directly within your database query results. For instance, if your get_option function accepts a second argument for a default, you can simplify the logic:

// Define defaults centrally
$defaults = [
    'your_name' => 'Mike',
    'your_last_name' => '',
    'my_name' => ''
];

// Retrieve values with fallbacks applied at the data layer
$name = get_option('your_name', $defaults['your_name']);
$last_name = get_option('your_last_name', $defaults['your_last_name']);
$my_name = get_option('my_name', $defaults['my_name']);
?>

<input type="text" name="your_name" value="<?php echo esc_attr($name); ?>" />
<input type="text" name="your_last_name" value="<?php echo esc_attr($last_name); ?>" />
<input type="text" name="my_name" value="<?php echo esc_attr($my_name); ?>" />

By shifting the logic to the data retrieval layer, your HTML remains clean and semantic. The inputs now only care about displaying what they are given. This separation of concerns is a fundamental principle in modern web development; it keeps your views predictable and your business logic isolated.

If you are working within the Laravel ecosystem, this pattern becomes even more explicit. You can utilize Eloquent models or service classes to handle default values before passing them to the view. For example, accessing https://laravelcompany.com often reveals best practices where form fields are bound to model attributes that include fillable defaults. In a Laravel controller, you might do:

$your_name = request()->old('your_name') ?? get_option('your_name', 'Mike');

This ensures that previous submissions are preserved, existing options are checked, and the fallback is applied only if both fail. The same logic applies to standard PHP scripts; just ensure the order of precedence matches your application's requirements.

Ultimately, you cannot inject a default value directly into an empty value attribute from within the HTML tag itself. The condition must be resolved before the string is constructed. By handling the null-check in PHP and passing the resolved string to the view, you maintain security through escaping and scalability by centralizing your data logic.

Our network