【问题标题】:Save html form data to a txt file using php使用php将html表单数据保存到txt文件
【发布时间】:2019-01-11 10:31:18
【问题描述】:

我是 PHP 新手并且正在苦苦挣扎,我有一种 HTML 表单类型的登录表单,我想将输入值从 html 表单存储到 txt 文件,这些所有文件都托管在免费托管站点上(我为学习目的)。我已经尝试了很多线程来实现这一点,但到目前为止它们都没有为我工作。所以任何人都可以帮助我,我已经尝试过代码。whihc 不起作用

一个 HTMl 表单,一个 php 代码文件和一个 txt 文件,其中三个托管在免费的虚拟主机 000webhost 上。

<!DOCTYPE html>
<html>

<BODY>

<form action = "post.php" method="POST">
    <h1> Please enter your information to create a new login account</h1>
    <p>
        <label>Login Name:</label><input type = "text"  name = "name" />
        <label>Password:</label><input type = "password" name = "pwd" />
        <br/>
        <br/>
    </p>
    <input type = "submit" name="submit_btn" id = "submit" value = "submit"/>
    <input type = "reset"  id = "reset" value = "reset"/>
</form>
</BODY>
</html>

php 文件

<?php

 if(isset($_POST['submit_btn']))
 {
  $username = $_POST['name'];
  $password = $_POST['pwd'];
  $text = $username . "," . $password . "\n";
  $fp = fopen('data.txt', 'a+');

    if(fwrite($fp, $text))  {
        echo 'saved';

    }
fclose ($fp);    
}
?>

我已经上传了data.txt。

【问题讨论】:

  • 你能在我的问题中看到吗?有一个包含登录表单的表单?还有一个 php 脚本来获取用户添加的这些值并将其保存到 txt 文件中?明确吗?
  • 您使用的是$_POST,但您的表单是通过GET发送的。
  • @JonStirling - 你好,我已经更改了我的代码,你能帮我吗?

标签: php html web-hosting forms


【解决方案1】:

这很可能是权限问题。使用is_writable检查写入权限或使用ini_set('display_errors', true);启用错误输出

相应地更改 data.txt 的权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多