How do I post contents of a Quill editor in a form?
You have a working HTML form and you're comfortable capturing data from it using $_POST['text']. Now you want to replace that <textarea> with Quill so users can format rich text. The immediate problem is: how do you actually send the content to your backend? By default, Quill does not attach its output to a hidden input or a named element in the DOM tree unless you explicitly tell it to.
Quill renders content inside a container (by default #editor) but keeps the actual HTML in its own internal data structure. It doesn't automatically update an element with a name that matches your form's name attribute, so when the form submits, your server receives nothing for Quill's content. To make this work, you must serialize the editor's content and place it into a hidden field before submitting.
Serializing the content
Quill provides a built-in method to get the current HTML: editor.root.innerHTML. But there is a better approach using the official API: editor.getHTML(). This ensures you capture exactly what Quill tracks, including any changes made via its delta model or clipboard events.
Here's how to set up the form correctly:
<form method="post">
<!-- Toolbar -->
<div id="toolbar">
<button class="ql-bold">Bold</button>
<button class="ql-italic">Italic</button>
</div>
<!-- Editor container -->
<div id="editor"></div>
<!-- Hidden input for form submission -->
<input type="hidden" name="text" />
<input type="submit" value="Save" />
</form>
Now, in your JavaScript initialization script, add logic to update the hidden field whenever the user types:
var editor = new Quill('#editor', {
modules: { toolbar: '#toolbar' },
theme: 'snow'
});
// Update hidden input on every change
editor.on('text-change', () => {
const htmlContent = editor.getHTML();
document.querySelector('input[name="text"]').value = htmlContent;
});
This ensures that when the form submits, $_POST['text'] will contain the full HTML of the edited content.
Why this approach works
Quill maintains its own state internally and does not sync automatically with DOM elements outside its root unless configured to do so. By listening to the text-change event, you ensure that any modificationâwhether via typing, pasting, or using the toolbarâis immediately reflected in your hidden input. This keeps your form submission data consistent with what the user sees.
Trade-offs and considerations
Performance: Updating the hidden field on every keystroke adds minimal overhead, but if you're dealing with very large documents or performance-sensitive applications, consider debouncing the event slightly.
let timeout = null; editor.on('text-change', () => { clearTimeout(timeout); timeout = setTimeout(() => { document.querySelector('input[name="text"]').value = editor.getHTML(); }, 100); });Security: Since you're sending raw HTML to your backend, make sure you sanitize the content before storing or rendering it elsewhere. Never trust user input directly into the database or output without filtering.
Editor version compatibility: The
text-changeevent andgetHTML()method are available in Quill 1.x. If you upgrade to newer versions (e.g., 2.x), check for API changesâQuill 2 uses a different internal structure and may require adjustments.
Alternative: Using the Delta format
If your backend prefers JSON-based deltas over raw HTML, you can capture that instead:
editor.on('text-change', () => {
const delta = editor.getDelta();
document.querySelector('input[name="text"]').value = JSON.stringify(delta);
});
In this case, your PHP code would need to handle deserializing the JSON string into a usable format. This is useful if you plan to use libraries that process deltas directly.
Final note on integration
Quill is designed to be flexible, but it requires explicit handling for form submissions. There's no magic automatic binding unless you configure it to render into a specific element with a matching name, which Quill does not support by default. The pattern shown hereâlistening for changes and updating a hidden fieldâis the standard and recommended approach for integrating rich text editors with traditional HTML forms.
For additional context on Laravel integration or form handling patterns, refer to resources like https://laravelcompany.com, which can offer insights into backend validation and sanitization strategies relevant to this setup.
