[英]How to Save data from a HTML/PHP Form to a Text file
I have a form that sends information to an email via a php file.我有一个通过 php 文件将信息发送到电子邮件的表单。
My HTML form is as below:我的 HTML 表单如下:
<form method="post" action="start-project.php">
<fieldset>
<span><input type="text" name="name" id="name" placeholder="Your Name" required/></span>
<span><input type="email" name="email" id="email" placeholder="Email Address" required/></span>
<span><input type="text" name="phone" id="phone" placeholder="Phone Number" required/></span>
<span><input type="text" name="website" id="website" placeholder="Website (Optional)" /></span>
</fieldset>
<span><input type="submit" name="submit" value="GET STARTED"></span>
</form>
and my php code is as below:我的php代码如下:
<?php
$EmailFrom = "email@domain.com";
$EmailTo = "receiver@email.com";
$Subject = "Submission: Start a Project";
$name = Trim(stripslashes($_POST['name']));
$phone = Trim(stripslashes($_POST['phone']));
$email = Trim(stripslashes($_POST['email']));
$website = Trim(stripslashes($_POST['website']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $phone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Website: ";
$Body .= $website;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=sent.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
How can I have the entry data to be save in a text file on the server?如何将条目数据保存在服务器上的文本文件中? I have seen few articles but I don't know how to implement it with my code.
我看过几篇文章,但我不知道如何用我的代码实现它。
I appreciate any help.我很感激任何帮助。
$log = file_put_contents('logs.txt', $name.PHP_EOL , FILE_APPEND | LOCK_EX);
If you use fopen() on a file that does not exist, it will create it,如果您对不存在的文件使用 fopen(),它将创建它,
$file= "file.txt";
$fh = fopen($file, 'a') or die("can't open file");
$stringData = "new data 1\n";
fwrite($fh, $stringData);
$stringData = "New data 2\n";
fwrite($fh, $stringData);
fclose($fh);
Thanks everyone who helped.感谢所有帮助过的人。
I found this solution and it worked fine with me.我找到了这个解决方案,它对我来说很好用。 I am putting it here in case if anyone came across it.
我把它放在这里以防万一有人遇到它。
I added the code below in my php code after opening of PHP tag:打开 PHP 标签后,我在我的 php 代码中添加了以下代码:
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['phone']) && isset($_POST['website'])) {
$data = $_POST['name'] . ' - ' . $_POST['email'] . ' - ' . $_POST['phone'] . ' - ' . $_POST['website'] . "\n";
$ret = file_put_contents('file.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.