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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17868399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:35:59  来源:igfitidea点击:

write to a text file from HTML form using PHP

phphtmlformstext

提问by user2620377

I wrote a html form just with an email field. I want to transfer that email that the user inputs into a text file on my web server.

我只写了一个带有电子邮件字段的 html 表单。我想将用户输入的电子邮件传输到我的 Web 服务器上的文本文件中。

When I click on the submit button I get a white screen. Nothing is written to my text file. I also want to redirect the user to another page when they click the submit button. Is this possible? I am a newbie.

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

<?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>

回答by user4035

In addition to what Andy G said about changing the name of submit button:

除了 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/");

Don't leave any blank lines between <?phpand the beginning of the file, otherwise redirect won't work.

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

回答by jh314

The submission results will be stored in the $_POSTvariable:

提交结果将存储在$_POST变量中:

<?php

$file = fopen("emaillist.txt","a+");

$email = $_POST['email']; 

fwrite($file,$email);
fclose($file); 
print_r(error_get_last());

?>

回答by Khawer Zeshan

Try this, Use different name for submit button also check if the submit button was pressed than write to file. You also didn't get the posted emailvalues

试试这个,为提交按钮使用不同的名称还检查提交按钮是否被按下而不是写入文件。你也没有得到发布的email

<?php
if(isset($_POST['submit']))
{
    $email = $_POST['email'];
    $file = fopen("emaillist.txt","a+");
    fwrite($file,$email);
    fclose($file); 
    print_r(error_get_last());
}
?>
<form action= "" method="post" name="form">
<input type="text" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>

回答by David

You have a few things you'll need to update here:

您需要在此处更新一些内容:

Form elements need to have different names, so you can differentiate between them in the PHP server-side code. Something like this:

表单元素需要有不同的名称,以便您可以在 PHP 服务器端代码中区分它们。像这样的东西:

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

You need to wrap your server-side code in a conditional to make sure it's a form post. Currently, when you first load this page, it's going to write to the file. But there's no content until you post back to the page again with the form data. So you need to check if it's a form post. A common way to do this is to check for the presence of the submit button in the post data:

您需要将服务器端代码包装在一个条件中,以确保它是一个表单帖子。目前,当您第一次加载此页面时,它将写入文件。但是在您使用表单数据再次回发到页面之前,没有内容。所以你需要检查它是否是一个表单帖子。一种常见的方法是检查帖子数据中是否存在提交按钮:

if (isset($_POST["emailSubmit"])) {
    $file = fopen("emaillist.txt","a+");
    fwrite($file,$email);
    fclose($file); 
    print_r(error_get_last());
}

Just to confirm, since you say you're new to PHP... You're expecting the file to be written on the serverand not on the client, correct?

只是为了确认一下,既然你说你是 PH​​P 的新手......你希望文件写在服务器上而不是在客户端上,对吗?

To redirect to another page, you can simply set the location header in the server-side PHP code. This needs to be done before any content is emitted to the client. Since you only want this to happen on a post event, it should be in the same conditional as before:

要重定向到另一个页面,您只需在服务器端 PHP 代码中设置位置标头即可。这需要在任何内容发送到客户端之前完成。由于您只希望在 post 事件上发生这种情况,因此它的条件应与以前相同:

if (isset($_POST["emailSubmit"])) {
    $file = fopen("emaillist.txt","a+");
    fwrite($file,$email);
    fclose($file); 
    print_r(error_get_last());

    header("Location: http://www.yoursite.com/newPage.php") ;
}

回答by Jinoj

<?php
if (isset($_POST['emailSubmit'])) {
    $file = fopen('emaillist.txt','a+');
    $email = $_POST['nail'];
    $fmail = $email.PHP_EOL;
    fwrite($file,$fmail);
    fclose($file); 
    print_r(error_get_last());

    header("Location: http://www.yourdomain.com/thanks.html") ;
}

?>
<form action='emaillist.php' method='post'>
    <input type='text' name ='nail' size= '30'/>
    <br />
    <br />
    <input type='submit' name='emailSubmit' value='OK'/>
    <br />
</form>

回答by azrosen92

In php theres an array called $_POSTthat stores all of the variables from a form when you post it. So $emailwould be found in $_POST['email']

在 php 中有一个名为的数组$_POST,用于在您发布表单时存储表单中的所有变量。所以$email会在$_POST['email']

回答by Andy G

Your submit button and input element and the form, have the same name "email". The first thing to do is to give them different names.

您的提交按钮和输入元素以及表单具有相同的名称“电子邮件”。首先要做的是给它们起不同的名字。

The name of the form also includes an extra space "email ".

表单的名称还包括一个额外的空格“电子邮件”。