Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将表单写入文本文件PHP_Php_Html - Fatal编程技术网

将表单写入文本文件PHP

将表单写入文本文件PHP,php,html,Php,Html,我是新来的 无论如何,我正在开发一个应用程序系统,它将你对表单的回答记录到一个名为data.txt的文本文件中。它工作了一段时间,然后我修改了一些东西,但它不再工作了 <html> <head> <link rel="stylesheet" href="style.css" <meta charset="utf-8"> <title>Apply</title> <link rel=

我是新来的

无论如何,我正在开发一个应用程序系统,它将你对表单的回答记录到一个名为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);
}
?>



您应该允许PHP执行重定向,而不是允许人们从您的站点欺骗他人。这就是说,通过处理POST数组,您可以稍微简化发布数据的收集。表单将提交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);
}
?> 


为什么要这样做?
onclick=“window.location.href=”http://pvpcity.dx.am/submitted/index.html';“
这是index.php中的代码吗?当他们提交时,将其重定向到另一个页面,这是index.php中的代码。当我尝试这段代码时,它工作正常……Hi-Ram,请稍微澄清一下您的代码,这是否创建了一个关联数组($payload),将$field name存储为键,将$value存储为值?这是一个规则的字符串数组(为了方便起见,请使用
sprintf
)在我看来,这样做的好处是,如果你需要在表单中添加字段,它们将被记录到这个文本文件中,而不需要额外的工作,这是很有意义的,我只是以前没有看到过,因为我是新使用PHP的!感谢你的清晰解释。
header('Location: http://pvpcity.dx.am/submitted/index.html');
<?php
if(!empty($_POST)){
    $txt = '';
    foreach ($_POST as $key => $value){
        $txt = $txt. " ##### ".$key.': '.$value;
    }
    file_put_contents('data.txt', $txt, FILE_APPEND);
}
?>