英文:
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
<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>
This is the php code I already wrote for redirecting.
<?php
if(isset($_POST['submit'])) {
// Fetching variable of the form which travels in URL
$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 "Please fill the email field! This is where you'll receive script files on.";?></span> <?php
}
}
?>
答案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() . ".txt";
file_put_contents($filename, $email);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论