如何将数据从HTML / PHP表单保存到文本文件

如何将数据从HTML / PHP表单保存到文本文件

问题描述:

I have a form that sends information to an email via a php file.

My HTML form is as below:

<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

$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 .= "
";
$Body .= "Tel: ";
$Body .= $phone;
$Body .= "
";
$Body .= "Email: ";
$Body .= $email;
$Body .= "
";
$Body .= "Website: ";
$Body .= $website;
$Body .= "
";

// 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.

我有一个表单,通过php文件向电子邮件发送信息。 p> 我的HTML表单如下: p>

 &lt; form method =“post”action =“start-project.php”&gt; 
&lt; fieldset&gt; 
&lt;  span&gt;&lt; input type =“text”name =“name”id =“name”placeholder =“Your Name”required /&gt;&lt; / span&gt; 
&lt; span&gt;&lt; input type =“email”name =  “email”id =“email”占位符=“电子邮件地址”/&gt;&lt; / span&gt; 
&lt; span&gt;&lt; input type =“text”name =“phone”id =“phone”placeholder =“Phone 编号“required /&gt;&lt; / span&gt; 
&lt; span&gt;&lt; input type =”text“name =”website“id =”website“placeholder =”Website(Optional)“/&gt;&lt; / span&gt;  
&lt; / fieldset&gt; 
&lt; span&gt;&lt; input type =“submit”name =“submit”value =“GET STARTED”&gt;&lt; / span&gt; 
&lt; / form&gt; 
  code>  
 
 

我的php代码如下: p>

 &lt;?php 
 
 $ EmailFrom =“email@domain.com”  ; 
 $ EmailTo =“receiver@email.com”; 
 $ Subject =“提交:启动项目 t“; 
 $ 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“&lt; meta http-equiv = \”refresh \“content = \”0; URL = error.htm \  “&gt;”; 
退出; 
} 
 
 //准备电子邮件正文
 $ Body =“”; 
 $ Body。=“名称:”; 
 $ Body。= $ name;  
 $ Body。=“
”; 
 $ Body。=“电话:”; 
 $ Body。= $ phone; 
 $ Body。=“
”; 
 $ Body。=“电子邮件 :“; 
 $ Body。= $ email; 
 $ Body。=”
“; 
 $ Body。=”网站:“; 
 $ Body。= $ website; 
 $ Body。=”  
“; 
 
 //发送电子邮件
 $ success = mail($ EmailTo,$ Subject,$ Body,”From:&lt; $ EmailFrom&gt;“); 
 
 //重定向到成功页面\  nif($ success){
 print“&lt; meta http-equiv = \”refresh \“content = \”0; URL = sent.php \“&gt;”; 
} 
else {
 print“&lt;  ; meta http-equiv = \“refresh \”content = \“0; URL = error.htm \”&gt;“; 
} 
?&gt; 
  code>  pre> 
 
  

如何将条目数据保存在服务器上的文本文件中? 我看过很少的文章,但我不知道如何用我的代码实现它。 p> 我感谢任何帮助。 p> div>

Have a look at file_put_contents()

 $log = file_put_contents('logs.txt', $name.PHP_EOL , FILE_APPEND | LOCK_EX);

You can append the data into a text file:

$myfile = fopen('files/file.txt', 'a');
fwrite($myfile, $data);
fclose($myfile);

php.net link

If you use fopen() on a file that does not exist, it will create it,

$file= "file.txt";
$fh = fopen($file, 'a') or die("can't open file");
$stringData = "new data 1
";
fwrite($fh, $stringData);
$stringData = "New data 2
";
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:

if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['phone']) && isset($_POST['website'])) {
    $data = $_POST['name'] . ' - ' . $_POST['email'] . ' - ' . $_POST['phone'] . ' - ' . $_POST['website'] . "
";
    $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');
}