2016-11-19 164 views
0

我有一個通過php文件向電子郵件發送信息的表單。如何將數據從HTML/PHP表單保存到文本文件

我的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> 

和我的PHP代碼如下:

<?php 

$EmailFrom = "[email protected]"; 
$EmailTo = "[email protected]"; 
$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\">"; 
} 
?> 

我怎麼能有所輸入的數據是在一個文本文件保存在服務器上? 我看過幾篇文章,但我不知道如何用我的代碼實現它。

我很感激任何幫助。

+0

就更不用說了,它的工作原理和發送電子郵件。我只是想要這個額外的選項將條目保存在文本文件中。 –

回答

0

謝謝大家誰幫助。

我發現這個解決方案,它對我工作得很好。 我把它放在這裏以防萬一有人遇到它。

我添加下面的代碼在我的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'); 
} 
3

看一看file_put_contents()

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

感謝您的關心與我分享。我不是先進的PHP代碼。我的PHP文件應該放在哪裏? –

+0

這一行將在您的文本文件中添加'$ name',然後轉到新行。你需要幾行不同的變量來確保你保存了所有你想保留的變量。 – Qrchack

+0

'.PHP_EOL'在'$ name','FILE_APPEND | LOCK_EX'確保你沒有刪除以前的文件,並鎖定文件,所以如果2個人同時發送郵件,你的腳本不會崩潰 – Qrchack

0

您可以將數據追加到一個文本文件:

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

php.net鏈接

1

如果使用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); 
相關問題