将post值写入文本文件


Writing post values to a text file

我正在尝试将字符串附加到文本文件中。

文件users.txt与php文件位于同一文件夹中。

$fname=$_POST['fname'];
$message = "name is :" . $fname. "'n";
if (isset($_POST['submit'])) {
    if ( file_exists("users.txt")) {
        $fp = fopen ("users.txt", "a");
        fwrite($fp, $message);
        fclose($fp);
    } else {
        echo'<script type="text/javascript">alert("file does not exist")</script>';
    }
}

它没有抛出消息错误,也没有写入文件
我做错了什么?

我的表格:

<form onsubmit="return ValidateInputs();" action="contact.php" method="post"  id="contactForm" >
          <input class="input" type="text"  name="fname" id="fname"  />
          <input    type="submit" value="submit" name="submit"  />
</form>

注:。这份表格确实提交了。。它会通过电子邮件将消息发送给我。

PHP处理程序的问题是没有定义fname变量。

添加了$fname=$_POST['fname'];

<?php
$fname=$_POST['fname'];
$message = "name is :" . $fname . "'n";
if (isset($_POST['submit'])) {
    if ( file_exists("users.txt")) {
        $fp = fopen ("users.txt", "a");
        fwrite($fp, $message);
        fclose($fp);
echo "Success";
    } 
else {
       echo'<script type="text/javascript">alert("file does not exist")</script>';
    }
}
?>

脚注:可能需要额外的权限才能将内容写入文件。CHMOD 644通常是标准权限设置,但是某些服务器需要777

通过FTP,如果有一个窗口可以手动键入命令,请在使用的FTP软件中键入chmod 644 users.txtchmod 777 users.txt

有关如何使用chmod文件的其他信息,可以访问基于Windows的托管服务的GoDaddy网站。

  • http://support.godaddy.com/help/article/6481/setting-directory-permissions-with-windows-hosting-accounts

这完全适用于我的本地主机。我还没有用HTML重新创建完整的场景,但它现在可以工作了。只需记住将文件权限设置为读/写,并确保一步一个脚印。例如,从这段代码开始,并确保获得带有所需文本的文件名file.txt。然后通过包含HTML位将其提升到下一个级别。

<?php
$fname = "Skippy";
$message = "name is :" . $fname. "'n";
$file = 'file.txt';
$fd = fopen($file, "a");
if ($fd) { 
    fwrite($fd, $message . "'n");   
    fclose($fd); 
}
?>