HTML 代码:
<form class="form-inline signup" role="form" action="script.php" method="post">
<div class="form-group">
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter your email address" name="email" >
</div>
<button type="submit" class="btn btn-theme">Get notified!</button>
</form>
PHP代码:
<?php
if (!empty($_POST["email"]))
file_put_contents("emails.txt", file_get_contents("emails.txt") . "'n" . $_POST["email"]);
?>
在这里询问之前,我尝试了几种方法,但没有一种有效。单击按钮时,它会加载 php 代码并在浏览器上显示一个空白页。
在
开始之前,你必须做很多事情。
将表单的
action
属性设置为.php
文件。<form class="form-inline signup" role="form" action="write.php" method="post">
为
<input />
元素指定一个name
属性。<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter your email address" name="email" />
在
write.php
(或您使用的任何PHP文件中),实际写入该文件。<?php if (!empty($_POST["email"])) file_put_contents("emails.txt", file_get_contents("emails.txt") . "'n" . $_POST["email"]); ?>
最后但并非最不重要的一点是,请在服务器上运行它,而不是在浏览器中运行:
http://localhost/file.php