所以我尝试了很多解决方案,很多不同的方法来写这篇文章,包括在网上和这里阅读。这是很简单的事情,但我不知道我做错了什么!
下面是表单的一个片段:
<form class="form-inline signup signup-form" role="form" action="submit-email.php" method="POST">
<div class="form-group">
<input type="email" class="form-control" id="Email1" name="Email1" placeholder="Enter your email address">
</div>
<button type="submit" class="btn btn-theme" value="Save Email">Get notified!</button>
</form>
和'submit-email.php'.
<?php
/**
* Trying to write the contents of the HTML form to .txt
*/
error_reporting(E_ALL); ini_set('display_errors', 1);
// All the values of the HTML form are securely stored in the array $v:
$v = array_map('trim', filter_input_array(INPUT_POST));
// Text formatting:
$text = '-- START ' . date('c') . ' --'n'
. "User email:{$v['email']}'n";
// Following lines of code open, write, and close your connection
// to a text file:
$file = 'emails.txt';
$fp = fopen($file, 'w');
fwrite($handle, $text);
fclose($fp);
另一个尝试:
<?php
$file = 'emails.txt'
$email = $_POST['Email1'];
$fp = fopen("emails.txt", "a");
$savestring = $email . "'n";
fwrite($fp, $savestring);
fclose($fp);
echo "<h1>Thank you, we will be in touch as soon as possible!</h1>";
仍然需要添加一个javascript弹出/警报,而不仅仅是echo。但如果有人能帮助与文件输出至少-这将是非常感激!
这里是错误-不读取'email'变量,即使我设置它(如果它不是正确的post:
Notice: Undefined index: email in /Applications/XAMPP/xamppfiles/htdocs/apa_dev/submit-email.php on line 12
Warning: fopen(emails.txt): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/apa_dev/submit-email.php on line 17
Notice: Undefined variable: handle in /Applications/XAMPP/xamppfiles/htdocs/apa_dev/submit-email.php on line 18
Warning: fwrite() expects parameter 1 to be resource, null given in /Applications/XAMPP/xamppfiles/htdocs/apa_dev/submit-email.php on line 18
Warning: fclose() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/apa_dev/submit-email.php on line 19
请记住,我已经尝试了很多不同的方法来写这个
你的html是好的。对于你的php来说,第二种方法更好,效果也不错。
只是在第2行末尾缺少一个分号(;)
$file = 'emails.txt' <--- missing semicolon(;)
注意:你没有使用第4行
中的$file变量但是你的代码有一个更简单的解决方案(相同的方法)。
写入文件可以使用"file_put_contents"它的功能与以下三个函数完全相同:fopen(), fwrite()和fclose()。
由于您希望在文件末尾继续写入,因此只需添加FILE_APPEND参数。
<?php
$file = 'emails.txt';
$email = $_POST['Email1'];
$data = "email: $email , whatever, more form data "; // here you can format your string for evry line of data; no need to put the new line here
file_put_contents($file, $data ."'n",FILE_APPEND); // new line is added here
echo "<h1>Thank you, we will be in touch as soon as possible!</h1>";
?>