所以我尝试过许多解决方案,包括在线阅读和在线阅读。这很简单,但我不知道我做错了什么!
以下是表单的摘录:
<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弹出/警报而不仅仅是回声。但如果有人可以至少帮助文件输出 - 非常感谢!
以下是错误 - 即使我设置了它也没有读取'email'变量(就好像它没有正确地发布一样:
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
请记住,我已尝试过许多不同的方法来写这篇文章。
答案 0 :(得分:0)
你的HTML确定无误。
对于你的PHP,你的第二种方法更好,它工作正常。
你只是在第2行的末尾有一个缺少的分号(;)
$file = 'emails.txt' <--- missing semicolon(;)
注意:您没有在第4行使用变量$ file
但是你的代码有更简单的解决方案(相同的方法)。
要写入文件,您可以使用&#34; file_put_contents&#34; 它与这三个函数完全相同: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>";
?>