Php $_POST method to get textarea value

Php $_POST method to get textarea value

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

Stefan Bogdanescu

Founder & Senior Architect

Why < and > Break Your PHP Echo Output

When you submit a form containing raw HTML tags within a textarea, the browser automatically strips those tags before sending the data to your server. This is standard behavior designed to prevent users from accidentally injecting malicious scripts or formatting code into your database. However, this default stripping mechanism creates a specific headache when your application needs to preserve those exact characters as plain text.

In the scenario you described, entering name1 <name@email.com> results in only name1 being echoed back in PHP. The browser has interpreted <name@email.com> not as literal text, but as the opening tag of an HTML element. Since the browser cannot close this tag (there is no corresponding </name@email.com>), it discards everything inside the "unclosed" tag to maintain document integrity. Consequently, your server receives only the text preceding the problematic tag.

To fix this, you must explicitly tell the browser to treat the content of that specific form field as plain text rather than HTML. You do this by adding the readonly attribute to the textarea element. When a textarea is marked as readonly, the browser assumes the user intends to store unformatted data and will not strip or escape any characters, including angle brackets.

Here is the corrected implementation:

<form method="post" action="index.php">
    <textarea 
        id="contact_list" 
        name="contact_list" 
        readonly>
    </textarea>
    <input type="submit" name="submit" value="Send" id="submit"/>
</form>

With this change, if a user enters "name1" <name@email.com>, the browser will pass the exact string name1 <name@email.com> to your PHP script. When you echo $contact_list in index.php, the output will now correctly display the email address without skipping it.

The Trade-Off: Read-Only vs. Editable

While adding the readonly attribute solves the immediate problem, it introduces a functional limitation. A readonly textarea prevents users from modifying the text inside the field once they have focused on it or clicked into it in some browsers, though it remains visible for editing in modern rendering engines. More importantly, you lose the ability to programmatically populate the textarea with default values via JavaScript before the form submits without triggering focus events.

If your application requires a dynamic user experience where users can edit the text after initial population, this approach is not viable. In that case, you would need to implement server-side sanitization to handle raw HTML input safely, which is a significantly more complex problem involving escaping functions and context-aware rendering.

Handling Raw Input in PHP

Once the browser successfully transmits the raw string with angle brackets, your PHP code can handle it normally using $_POST['contact_list']. The value will arrive as a string containing the literal characters < and >.

<?php
// index.php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = $_POST['contact_list'];
    echo htmlspecialchars($data); // Always escape output to prevent XSS
}
?>

It is crucial to remember that while the input is now raw, you must still use htmlspecialchars() when echoing it back to the user in a browser context. The browser stripped the tags on the way in because they were invalid HTML; if you echo them without escaping, you risk breaking your page layout or exposing yourself to Cross-Site Scripting (XSS) vulnerabilities if the content ever changes to include malicious scripts.

When to Use This Pattern

This specific technique—using readonly to preserve raw text—is particularly useful for logging systems, debug consoles, or fields where users paste code snippets or configuration strings that might contain HTML-like syntax. If you are building a platform like LaravelCompany, ensuring that data integrity is maintained between client input and server storage without altering the character set is essential for accurate record-keeping.

However, be aware that relying on browser quirks like tag stripping should generally be avoided in production logic. The readonly attribute provides a cleaner, more semantic solution by explicitly defining the data type as "plain text" rather than hoping the browser behaves a certain way when encountering malformed HTML.

Our network