Setting value of a HTML form textarea?

Setting value of a HTML form textarea?

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

Stefan Bogdanescu

Founder & Senior Architect

Why PHP Echo Inside HTML Attributes Fails to Populate Textareas

A common stumbling block for developers moving from static HTML to dynamic PHP is the syntax used to inject variables into form fields. The source code provided attempts to populate a <textarea> using an inline value attribute that directly interpolates a PHP variable:

<TEXTAREA NAME="message" COLS=40 ROWS=6 value="<?=$message;?>"></TEXTAREA>

Despite $message containing a non-null value from $_REQUEST, the textarea remains empty. The root cause is not that PHP failed to execute, but rather how the browser interprets the resulting HTML string. This specific pattern relies on the browser performing client-side variable substitution, which does not exist in standard HTML or JavaScript execution contexts.

The Browser's Interpretation of <?=

When you write a file containing PHP code, the server processes it and sends only the rendered result to the client. However, the logic inside your specific example creates a conflict between the server-side rendering engine and the browser's parsing rules.

The syntax <b>...</b> indicates this is an HTML document. While modern servers correctly parse <?= as a Short Tag Echo, older browsers or strictly configured environments might treat the entire block—including the PHP tags—as plain text if they do not recognize the PHP MIME type. More importantly, even if the server renders the output correctly, the resulting HTML sent to the browser depends entirely on how the PHP code is structured relative to the HTML tags.

In your example:

<TEXTAREA NAME="message" ... value="<?=$message;?>">...</TEXTAREA>

The browser does not know PHP. It only sees what it receives after the server has processed the file. If the server fails to process the file, or if the syntax is interpreted incorrectly by the parser, the value attribute will literally contain the string <?=$message;?>. When the browser parses this, it looks for a space before the closing quote of the attribute or before the tag closes. The presence of the PHP tags inside the HTML attribute value confuses the HTML parser, often leading it to ignore the content within the value attribute entirely or treat it as malformed syntax.

The Correct Approach: Server-Side String Concatenation

To reliably set the value of a textarea (or any input field), you must ensure the PHP variable is fully resolved into its string representation before it is injected into the HTML attribute. The echo statement inside the attribute is unnecessary and risky because it exposes the server's processing logic to the client's parser in a way that can break HTML structure.

The robust method is to assign the value to a variable using the concatenation operator or simply echo the content outside of the tag if you are building the string manually, but for inline attributes, direct assignment is safer:

$message = $_REQUEST['message'] ?? ''; // Default to empty string if not set

// Safe and standard way to inject the value
echo '<textarea name="message" cols="40" rows="6">';
echo htmlspecialchars($message);
echo '</textarea>';

While your original goal was likely to write a single-line HTML tag, sticking to that line requires ensuring the PHP variable is escaped. If you must keep it on one line:

$textareaValue = isset($_REQUEST['message']) ? $_REQUEST['message'] : '';
?>
<textarea name="message" cols="40" rows="6"><?php echo htmlspecialchars($textareaValue); ?></textarea>

Using htmlspecialchars() is critical here. Without it, if $message contains a single quote (') or a double quote ("), it will break the HTML attribute syntax, rendering the textarea empty or causing a parsing error. For instance, if the user submits It's a "test", the resulting HTML becomes invalid:

<textarea name="message" value="It's a "test">...

The browser stops parsing the attribute at the first unescaped quote. By escaping the content, you ensure the attribute boundaries remain intact regardless of the user input.

Handling Hidden Fields and File Uploads

Your snippet also includes an <input type="hidden" name="MAX_FILE_SIZE" value="1000000">. This is a legacy technique used in older versions of PHP to limit file upload sizes before processing began. In modern Laravel applications, this is handled automatically via the MultipartForm stream wrapper configuration and does not require manual hidden inputs for size limits.

If you are maintaining legacy code or working outside of a framework like Laravel, ensure that the $message variable is sanitized. Since you are pulling from $_REQUEST, which merges $_GET and $_POST, there is a risk of overwriting variables if form data and query parameters share the same name. Always check if the key exists before assigning:

if (isset($_REQUEST['message'])) {
    $message = $_REQUEST['message'];
} else {
    $message = '';
}

Conclusion on Implementation

The failure of your original code stems from placing raw PHP echo tags inside an HTML attribute without proper escaping or structural separation. The browser does not execute <?= as a variable resolver; it expects valid HTML attributes. By moving the logic to standard concatenation or using short tags correctly with htmlspecialchars(), you guarantee that the textarea displays exactly what the user submitted, preventing injection attacks and parsing errors.

Our network