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评论96阅读模式
英文:

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代码

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

这是我已经编写的用于重定向的PHP代码。

  1. <?php
  2. if(isset($_POST['submit'])) {
  3. // 从表单中获取变量
  4. $email = $_POST['email'];
  5. $myfile = fopen("text.txt", "w");
  6. fwrite($myfile, $email);
  7. fclose($myfile);
  8. if($email != '') {
  9. header("Location: https://www.formget.com/app/");
  10. }
  11. else {
  12. ?><span><?php echo "请填写电子邮件字段!这是您将接收脚本文件的地方。";?></span> <?php
  13. }
  14. }
  15. ?>

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

英文:

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

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

This is the php code I already wrote for redirecting.

  1. &lt;?php
  2. if(isset($_POST[&#39;submit&#39;])) {
  3. // Fetching variable of the form which travels in URL
  4. $email = $_POST[&#39;email&#39;];
  5. $myfile = fopen(&quot;text.txt&quot;, &quot;w&quot;);
  6. fwrite($myfile, $email);
  7. fclose($myfile);
  8. if($email !=&#39;&#39;) {
  9. header(&quot;Location:https://www.formget.com/app/&quot;);
  10. }
  11. else {
  12. ?&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
  13. }
  14. }
  15. ?&gt;

答案1

得分: 2

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

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

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

  1. $filename = uniqid() . &quot;.txt&quot;;
  2. 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:

确定