php - 使用 PHP 从 HTML 表单写入文本文件

标签 php html forms text

我写了一个只有电子邮件字段的 html 表单。我想转移用户发送的电子邮件 输入到我的网络服务器上的文本文件中。

当我点击提交按钮时出现白屏。没有任何内容写入我的文本文件。 我还想在用户单击提交按钮时将用户重定向到另一个页面。 这可能吗?我是新手。

<?php

$file = fopen("emaillist.txt","a+");
fwrite($file,$email);
fclose($file); 
print_r(error_get_last());

?>

<form action= "emaillist.php" method="post" name="email ">
<input type="text" name="email">
<br>
<br>
<input type="submit" name="email" value="submit"><br>

最佳答案

除了 Andy G 所说的更改提交按钮的名称之外:

<?php
//Get the email from POST
$email = $_REQUEST['email'];
$file = fopen("emaillist.txt","a+");
fwrite($file,$email);
print_r(error_get_last());

//redirect
header("Location: http://www.example.com/");

不要在<?php 之间留空行和文件的开头,否则重定向将不起作用。

关于php - 使用 PHP 从 HTML 表单写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17868399/

相关文章:

html - 布局在小屏幕笔记本电脑中不合适

python - 合并来自多个表单 Django 的数据

javascript - 为什么我的叠加层覆盖了一些元素而不是全部?

php - PHP 中的 POST 和原始 POST 有什么区别?

angularjs - 检查表单是否被触摸

php - 可以在 Travis CI 中使用自定义变量吗?

php - 防止 Javascript 解码编码的 HTML

php - 我需要在 php 中的数字之间使用逗号

php - 我需要使用单个查询将值插入到两个不同的表中

javascript - 如何将 html 转换为 JSX、ReactJS 组件?