What is the common error when trying to write data to a text file using a PHP form?

The common error when trying to write data to a text file using a PHP form is incorrect file permissions. To solve this issue, you need to ensure that the directory where the text file is located has the correct write permissions for the web server. You can set the permissions to 755 or 777 depending on your server configuration.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $data = $_POST['data'];
    $file = 'data.txt';

    if (file_put_contents($file, $data . PHP_EOL, FILE_APPEND) !== false) {
        echo "Data written successfully to $file";
    } else {
        echo "Unable to write data to $file";
    }
}
?>