Skip to content
PHP Logo

Are there best practices for setting default text values in textarea fields in PHP forms?

When setting default text values in textarea fields in PHP forms, it is important to sanitize the input to prevent any potential security vulnerabilities. One common approach is to use the htmlspecialchars() function to escape special characters and prevent cross-site scripting attacks. Additionally, you can use the ternary operator to check if the textarea field has a value submitted by the user, and if not, display the default text.

php
<textarea name="message"><?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message']) : 'Enter your message here'; ?></textarea>