You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何添加PHP代码实现点击提交按钮时始终新建文本文件并写入用户输入

实现每次提交创建新文件存储用户输入

需求说明

需要修改现有PHP代码,让用户点击「Submit」按钮时,每次创建一个新的文本文件来存储输入的邮箱地址,而非写入固定文件。

现有HTML代码(无需修改)

<form action="save_redirect.php" id="#form" method="post" name="#form">
  <label>Email:</label><br>
  <input id="email" name="email" placeholder='Enter A Valid Email Address' type='text'>
  <br>
  <input id='btn' name="submit" type='submit' value='Submit'>
  <?php
  include "include/redirect.php";
  ?>
</form>

修改后的PHP代码(save_redirect.php)

<?php
if(isset($_POST['submit'])) {
  // 获取表单提交的邮箱并去除首尾空白
  $email = trim($_POST['email']);

  // 生成唯一文件名:时间戳+唯一标识符确保不重复
  $filename = 'email_' . time() . '_' . uniqid() . '.txt';
  
  // 创建新文件并写入内容
  $myfile = fopen($filename, "w");
  fwrite($myfile, $email);
  fclose($myfile);

  if(!empty($email)) {
    header("Location:https://www.formget.com/app/");
    exit; // 跳转后终止脚本,避免后续代码执行
  }
  else {
    ?>
    <span><?php echo "请填写邮箱字段!我们会将脚本文件发送到这个邮箱。";?></span>
    <?php
  }
}
?>

关键修改说明

  • 唯一文件名生成:用time()获取当前时间戳,搭配uniqid()生成唯一标识,保证每次提交的文件名完全不同,不会覆盖已有文件。
  • 输入内容处理:添加trim()去除邮箱前后空白,避免无效的空内容写入文件。
  • 脚本终止优化:跳转后添加exit语句,防止后续代码意外执行引发问题。

内容的提问来源于stack exchange,提问作者Johnny blee Rowland

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 05:17:04