【问题标题】:Save HTML form on server using PHP使用 PHP 在服务器上保存 HTML 表单
【发布时间】:2022-06-16 01:02:11
【问题描述】:

我正在尝试创建一个简单的表单,供用户在网站上输入详细信息并将条目保存到文本文件中。

我有一个简单的测试页面

<html>
<head>
        <title>Save POST Data</title>
</head>
<body>

<form action="log.php" name="form" method="post">
      <input type="text" name="firstName" placeholder="First Name" required autocomplete="off"> <br>
      <input type="text" name="lastName" placeholder="Last Name" required autocomplete="off"> <br>
      <input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

写结果的php文件

<?php
if(isset($_POST['submit'])){
$firstName = "First Name:".$_POST['firstName']."
";
$lastName = "Last Name:".$_POST['lastName']."
";
$file=fopen("/home/user/file.txt", "a");
fwrite($file, $firstName);
fwrite($file, $lastName);
fclose($file);
}
?>

fastcgi 包含在我的 Nginx 站点配置文件中

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}

我看到的所有例子都是这种方法的变体。但是没有任何内容写入文件。我正在尝试的实际上可能吗?

【问题讨论】:

  • 应该是,但是您是否有特定的原因要将数据存储在文件中而不是数据库中?信息存储后您打算如何处理?

标签: php forms nginx


猜你喜欢
  • 2017-09-04
  • 1970-01-01
  • 2011-07-07
  • 2017-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
  • 1970-01-01
相关资源
最近更新 更多