[英]Writing form to a text file PHP
我是新来的。
无论如何,我正在开发一个应用程序系统,它将您对表单的回答记录到一个名为 data.txt 的文本文件中。它工作了一段时间,然后我修补了一些东西,它不再工作了。
<html>
<head>
<link rel="stylesheet" href="style.css"
<meta charset="utf-8">
<title>Apply</title>
<link rel="icon" type="image/png" href="favicon.png">
</head>
<body>
<h1 class="unselectable">PvPCity Application</h1>
<form target=_blank action="index.php" method="post">
<input type="text" name="IGN" placeholder="IGN" autocomplete="off"><br>
<input type="text" name="Discord" placeholder="Discord" autocomplete="off"><br>
<input type="text" name="Age" placeholder="Age" autocomplete="off">
<textarea name="Why" placeholder="Why do you want to be staff?" autocomplete="off"></textarea>
<input onclick="window.location.href = 'http://pvpcity.dx.am/submitted/index.html';" type="submit" value="Apply" />
</form>
</body>
</html>
<?php
if( isset($_POST['IGN'] ) && isset( $_POST['Discord'] ) && isset( $_POST['Age'] ) && isset( $_POST['Why'] ) )
{
$txt=' ##### IGN: '.$_POST['IGN'].' ##### Discord: '.$_POST['Discord'].' ##### Age: '.$_POST['Age'].' ##### Why: '.$_POST['Why'] . PHP_EOL;
file_put_contents('data.txt', $txt, FILE_APPEND);
}
?>
这是代码。 如果你能告诉我出了什么问题,那就太好了。 这里的代码来自 index.php 提交按钮在点击时重定向到不同的页面。 老实说,我不知道为什么它不起作用
您应该允许 PHP 执行重定向,而不是允许人们从您的站点潜在地欺骗他人。 也就是说 - 您可以通过处理 POST 数组来稍微简化 POSTed 数据的收集。 表单将提交 OK 并重定向,但远程站点可能会不准确地报告应用程序已提交 - 大概这也将托管在该站点上,因此不一定是问题。
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
$payload=array();
foreach( $_POST as $field => $value )$payload[]=sprintf('##### %s %s', $field, $value );
file_put_contents( 'data.txt', implode( ' ',$payload ).PHP_EOL, FILE_APPEND );
header('Location: http://pvpcity.dx.am/submitted/index.html');
}
?>
<html>
<head>
<link rel="stylesheet" href="style.css"
<meta charset="utf-8">
<title>Apply</title>
<link rel="icon" type="image/png" href="favicon.png">
</head>
<body>
<h1 class="unselectable">PvPCity Application</h1>
<form target=_blank method="post">
<input type="text" name="IGN" placeholder="IGN" autocomplete="off"><br>
<input type="text" name="Discord" placeholder="Discord" autocomplete="off"><br>
<input type="text" name="Age" placeholder="Age" autocomplete="off">
<textarea name="Why" placeholder="Why do you want to be staff?" autocomplete="off"></textarea>
<input type="submit" value="Apply" />
</form>
</body>
</html>
我相信您不需要onclick="window.location.href = ...
您的提交可以转到同一个文件 (index.php),您只需要一个更好的条件,并且由于您的输入未标记为必需,您可能会使用像这样:
<?php
if(!empty($_POST)){
$txt = '';
foreach ($_POST as $key => $value){
$txt = $txt. " ##### ".$key.': '.$value;
}
file_put_contents('data.txt', $txt, FILE_APPEND);
}
?>
<html>
<head>
<link rel="stylesheet" href="style.css"
<meta charset="utf-8">
<title>Apply</title>
<link rel="icon" type="image/png" href="favicon.png">
</head>
<body>
<h1 class="unselectable">PvPCity Application</h1>
<form target=_blank action="" method="post">
<input type="text" name="IGN" placeholder="IGN" autocomplete="off"><br>
<input type="text" name="Discord" placeholder="Discord" autocomplete="off"><br>
<input type="text" name="Age" placeholder="Age" autocomplete="off">
<textarea name="Why" placeholder="Why do you want to be staff?" autocomplete="off"></textarea>
<input type="submit" value="Apply" />
</form>
</body>
</html>
<?php
if( isset($_POST['IGN'] ) && isset( $_POST['Discord'] ) && isset( $_POST['Age'] ) && isset( $_POST['Why'] ) )
{
$txt=' ##### IGN: '.$_POST['IGN'].' ##### Discord: '.$_POST['Discord'].' ##### Age: '.$_POST['Age'].' ##### Why: '.$_POST['Why'] . PHP_EOL;
if(file_put_contents('data.txt', $txt, FILE_APPEND)){
header('Location: http://pvpcity.dx.am/submitted/index.html');
}else{
echo 'data is not saved';
}
}
?>
我在上面的代码中做了一些更改以重定向到另一个网站。 您不想在点击时重定向,您可以通过 PHP header() 来实现。
header('Location: http://pvpcity.dx.am/submitted/index.html');
这将在提交表单后重定向...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.