如何将数据从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.

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');
}