How do I add a PHP code that will always open/create a new text file and write user input once user clicks 'Submit' button

huangapple go评论72阅读模式
英文:

How do I add a PHP code that will always open/create a new text file and write user input once user clicks 'Submit' button

问题

Here is the translation of the provided content:

如何添加一个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代码。

<?php
if(isset($_POST['submit'])) {
  // 从表单中获取变量
  $email = $_POST['email'];
  $myfile = fopen("text.txt", "w");
  fwrite($myfile, $email);
  fclose($myfile);

  if($email != '') {
    header("Location: https://www.formget.com/app/");
  }
  else {
    ?><span><?php echo "请填写电子邮件字段!这是您将接收脚本文件的地方。";?></span> <?php
  }
}
?>

请注意,我只提供了翻译的部分,没有包括代码部分。如果您需要代码的翻译,请提供特定的代码行并要求翻译。

英文:

How do I add a PHP code that will always open/create a new text file and write user input once user clicks 'Submit' button.

This is the html code

&lt;form action=&quot;save_redirect.php&quot; id=&quot;#form&quot; method=&quot;post&quot; name=&quot;#form&quot;&gt;
  &lt;label&gt;Email:&lt;/label&gt;&lt;br&gt;
  &lt;input id=&quot;email&quot; name=&quot;email&quot; placeholder=&#39;Enter A Valid Email Address&#39; type=&#39;text&#39;&gt;
  &lt;br&gt;
  &lt;input id=&#39;btn&#39; name=&quot;submit&quot; type=&#39;submit&#39; value=&#39;Submit&#39;&gt;
  &lt;?php
  include &quot;include/redirect.php&quot;;
  ?&gt;
&lt;/form&gt;

This is the php code I already wrote for redirecting.

&lt;?php
if(isset($_POST[&#39;submit&#39;])) {
  // Fetching variable of the form which travels in URL
  $email = $_POST[&#39;email&#39;];
  $myfile = fopen(&quot;text.txt&quot;, &quot;w&quot;);
  fwrite($myfile, $email);
  fclose($myfile);

  if($email !=&#39;&#39;) {
    header(&quot;Location:https://www.formget.com/app/&quot;);
  }
  else {
    ?&gt;&lt;span&gt;&lt;?php echo &quot;Please fill the email field! This is where you&#39;ll receive script files on.&quot;;?&gt;&lt;/span&gt; &lt;?php
  }
}
?&gt;

答案1

得分: 2

使用 uniqid() 生成唯一的文件名。然后,您可以使用 file_put_contents() 来组合 fopen()/fwrite()/fclose()

$filename = uniqid() . ".txt";
file_put_contents($filename, $email);
英文:

Use uniqid() to generate a unique filename. And you can use file_put_contents() to combine fopen()/fwrite()/fclose().

$filename = uniqid() . &quot;.txt&quot;;
file_put_contents($filename, $email);

huangapple
  • 本文由 发表于 2023年6月21日 23:50:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定