Hello,

I'm looking for a way to make a simple multi line text box in PHP. I am trying to make a text box a couple thousand characters log. but when i try to create it, it just makes a very long text box. I can't seem to find any information on how to make that a multi line box. I don't care if you can use line returns or not.

I'm very very new to PHP.

Dani AI

Generated

Quick practical note tying the thread together: and were right to point at the HTML control for multiline text. Modern HTML (HTML5) lets you set a browser-enforced character limit with the maxlength attribute on <textarea> (the limit is measured in UTF‑16 code units). If you want a quick, browser-side limit, use that first. (developer.mozilla.org)

Example (simple HTML):

<form method="post" action="save.php">
  <textarea id="body" name="body" rows="8" cols="60" maxlength="2000"></textarea>
  <input type="submit" value="Submit">
</form>

The element also exposes a maxLength property you can read/change from JavaScript. Use JS for a nicer UX (live counter, prevention while typing) or for older browsers that might behave differently. was right that JS is a common fallback. (developer.mozilla.org)

Minimal JS fallback + counter idea:

const ta = document.getElementById('body');
const max = ta.maxLength;
const counter = document.getElementById('counter'); // optional span

ta.addEventListener('input', () => {
  if (ta.value.length > max) ta.value = ta.value.slice(0, max);
  if (counter) counter.textContent = `${ta.value.length}/${max}`;
});

Important security note: never rely only on client-side limits. Validate and enforce length on the server before processing or storing the value (client-side checks can be bypassed). For UTF-8 text prefer mb_strlen() when checking visible characters; if you need true grapheme-cluster counts (emojis that combine), use grapheme_strlen() from the intl/grapheme functions. Also escape output with htmlspecialchars() when rendering back to HTML. Example PHP check:

$body = $_POST['body'] ?? '';
if (mb_strlen($body, 'UTF-8') > 2000) { die('Input too long'); }
echo nl2br(htmlspecialchars($body, ENT_QUOTES, 'UTF-8'));

References for behavior and secure handling: HTML <textarea>/maxlength, server-side input validation guidance (OWASP), and PHP string/escaping functions. (developer.mozilla.org)

Recommended Answers

All 4 Replies

I'm a bit confused by your request. Text boxes are generated in html and the data sumbitted is used by the PHP script. If you want to display a text box on a .php page, simply close the php tags and create an html form. I use the <textarea> tag to create mine. You can open and close the <?php tags whenever you like throughout the page. When the tag is closed ?> you can treat the file like a normal html page.

If that isn't what you meant post more details and I'll try to troubleshoot with you. Hope that helps.

Zechieo - Lanslide Gaming PCs:
The <FAKE SIGNATURE>Built For LAN Parties

^yes. <textarea> can be used to create multi-line text box

My fault. I am very new to all of this.. I think I figured it out though. After looking a little more around on the net.. I am creating an html form. I was using the textarea tag wrong.

The only thing that i can't seem to figure out. Is there a way to limit the size of the textarea in characters?

You would have to use JavaScript I beleive I don't think the textarea allows the maxlength attribute.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.