将表单保存为txt文件。文本未显示,原因是?

huangapple go评论72阅读模式
英文:

saving form to txt. text does not show, why?

问题

我在我的网站上有一个表单。我想要将输入保存到txt文件中,但是当我提交数据时,.txt文件变得更大(字节增加),但没有文本显示出来。
以下是代码:

<div>
    <form action="controller.php">
        <input name="card" id="card" type="email">
        <button type="submit" name="submit">提交</button>
    </form>
</div>

以下是.php代码:

<?php                
    $card = $_POST['card'];
    $file = fopen('file.txt', "a");
    fwrite($file, $card . "\n");
    fclose($file);
    die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>

我在子域名上使用相同的代码,它完美运行。为什么它在这里不起作用,我该如何修复?

我尝试更改了ID和类型。

英文:

I have a form on my website. I wanted to save to input to txt but when I submit data, the .txt file get larger (bytes increase) but no text shows up,
Here is the code

	&lt;div&gt;
	&lt;form action=&quot;controller.php&quot;&gt;
			&lt;input name=&quot;card&quot; id=&quot;card&quot; type=&quot;email&quot;&gt;
			&lt;button type=&quot;submit&quot; name=&quot;submit&quot; &gt;Submit&lt;/button&gt;
                   &lt;/form&gt;
                   &lt;/div&gt;

Here is the .php code

&lt;?php                
    $card = $_POST[&#39;card&#39;];
    $file = fopen (&#39;file.txt&#39;, &quot;a&quot;);
    fwrite($file, $card . &quot;\n&quot;);
    fclose($file);
     die(header(&quot;Location: &quot;.$_SERVER[&quot;HTTP_REFERER&quot;]));
?&gt;

I have a sub domain with the same code and it works perfectly fine.
why is it not working and how do I fix?

I tried to change the id and the type

答案1

得分: 1

你在这里有一个非常简单的问题。让我们看看为什么,尝试在你的文件上执行以下操作:

print_r($_POST);

你会发现,在提交表单后,你没有POST数据。你的表单默认情况下(根据你的服务器或PHP配置)不是使用POST方法发送数据,而是使用GET方法(你的其他服务器可能设置相反)。

要解决这个问题,你可以将你的$card变量更改为:

$card = $_GET['card'];

或者,更好的选择是,为了更清晰并且在将网站迁移到另一个服务器/PHP版本时避免问题,你可以在你的<form>标签上指定方法:

<form action="controller.php" method="post">

请不要忘记保护将写入此文件的数据,恶意用户存在,如果保持你的代码如此简单,可能会造成严重的安全问题。

英文:

You have a very simple issue here. Let's see why by trying to do this on your file:

print_r($_POST);

You will see that after posting your form, you have no POST data. Your form is, by default (on your server or PHP configuration) sending the data not using the POST method, but the GET one (your other server probably is setup the opposite way).

To fix this, you can either change your $card variable to:

$card = $_GET[&#39;card&#39;];

Or rather, and that would be a better option to make it more clear and avoid problems if you migrate your website on another server/PHP version, you could simply specify the method on your <form> tag:

&lt;form action=&quot;controller.php&quot; method=&quot;post&quot;&gt;

Please, don't forget to secure the data that will be written in this file, malicious users exist and if you keep your code that simple, it might be a serious security issue.

huangapple
  • 本文由 发表于 2023年1月9日 06:04:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定