How to create a text box in PHP to output data

How to create a text box in PHP to output data

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

Stefan Bogdanescu

Founder & Senior Architect

Displaying Data in HTML Text Boxes from PHP Functions

Displaying data returned by a PHP function requires bridging two distinct layers: the server-side logic that fetches information and the client-side markup that renders it to the user. The core misunderstanding often lies in expecting CSS or PHP alone to handle the display; instead, you need to output valid HTML elements within your response stream.

When your PHP function looks up data from a SQL Server and returns a value—typically via echo or by returning a string—the raw output is just text unless explicitly wrapped in an HTML container. To create a "text box" (technically an input field with read-only attributes or a simple div styled as such), you must generate the corresponding HTML tags.

The Basic Implementation

The most straightforward way to display data inside a visual box is using an <input> element. Since the goal is confirmation of data retrieval rather than user entry, you should disable editing capabilities. PHP variables can be interpolated directly into the value attribute of the input tag.

Here is a standard pattern for a function that retrieves a user name and displays it in a read-only text box:

function getUserData() {
    // Simulated database lookup
    $userName = "john_doe"; 
    
    return $userName;
}

// Output the data inside a styled input element
$value = getUserData();
echo '<input type="text" value="' . htmlspecialchars($value) . '" readonly>';

In this example, type="text" defines the control as a single-line text field. The readonly attribute is crucial; it prevents users from modifying the data while keeping it selectable and pasteable, which distinguishes it from a disabled field that might appear grayed out depending on browser defaults.

Styling with CSS

While raw HTML works, adding CSS transforms the plain input into a polished text box. You can apply styles inline for quick prototypes or in an external stylesheet for maintainability.

To make the box visually distinct, you might target all read-only inputs within your page:

input[readonly] {
    background-color: #f0f0f0;
    border: 1px solid #ccc;
    padding: 8px;
    font-family: sans-serif;
    border-radius: 4px;
}

This CSS rule ensures the box has a subtle gray background and rounded corners, mimicking modern UI patterns. If you need to handle specific values dynamically, remember that inline style attributes work well for unique cases, such as highlighting a critical error message in red:

$errorMessage = "Connection failed";
echo '<input type="text" value="' . htmlspecialchars($errorMessage) . '" readonly style="color: red; background-color: #ffcccc;">';

Security Considerations and Best Practices

A critical pitfall when outputting user data or database values into HTML is Cross-Site Scripting (XSS). If your SQL query returns a string that includes quotes or script tags, directly echoing it can break your HTML structure or allow malicious code to execute.

Always sanitize output using htmlspecialchars(). This function converts special characters like <, >, and & into their HTML entity equivalents (&lt;, &gt;, &amp;). By doing so, you ensure that even if the database contains a string like "<script>alert(1)</script>", it will appear as literal text inside your PHP-generated input box rather than executing code.

Modern Alternatives with Laravel

If your project is built on the Laravel framework, handling this display logic becomes more structured. Blade templates allow you to separate view logic from your functions. You might create a component that accepts data and renders it safely:

{{-- components/data-display.blade.php --}}
<input type="text" value="{{ $data }}" readonly class="form-control">

This approach leverages Laravel's built-in escaping mechanisms when using double braces in Blade, reducing the risk of manual sanitization errors. For more complex forms or dynamic inputs, Laravel's form helpers provide utilities that manage validation and CSRF protection automatically. If you are looking to integrate these patterns into an existing PHP application or need guidance on upgrading your stack, resources like https://laravelcompany.com can offer insights into modernizing legacy PHP codebases with robust frameworks.

Ultimately, creating a text box for data output is less about complex CSS and more about correctly combining PHP echo statements with safe HTML generation. By using readonly inputs and sanitizing your output, you achieve a secure and user-friendly display of your database results.

Our network