简体   繁体   English

将表单写入文本文件 PHP

[英]Writing form to a text file PHP

I'm new here.我是新来的。

Anyway, I'm working on an application system that logs your answers to the form into a text file called data.txt It was working for a while and then I tinkered with some stuff and it's not working anymore.无论如何,我正在开发一个应用程序系统,它将您对表单的回答记录到一个名为 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);
}
?>

Here is the code.这是代码。 If you could tell me what is wrong that would be great.如果你能告诉我出了什么问题,那就太好了。 The code here is from index.php The submit button redirects to a different page when you click.这里的代码来自 index.php 提交按钮在点击时重定向到不同的页面。 I honestly have no idea why it's not working老实说,我不知道为什么它不起作用

You should allow PHP to perform the redirection rather than allow people to potentially spoof others from your site.您应该允许 PHP 执行重定向,而不是允许人们从您的站点潜在地欺骗他人。 That said - you can simplify the collection of POSTed data a little by processing the POST array.也就是说 - 您可以通过处理 POST 数组来稍微简化 POSTed 数据的收集。 The form will submit OK and redirect but the remote site will perhaps inaccurately report back that the application has been submitted - presumably this will be hosted on that site also so not necessarily an issue.表单将提交 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>

I believe you don't need onclick="window.location.href = ... Your submission can go to same file (index.php), you just need a better condition and since your inputs are not marked as required you might use something like this:我相信您不需要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';
 }
}
?>

I did some alteration in that above code for redirect to another website.我在上面的代码中做了一些更改以重定向到另一个网站。 You don't want to redirect on click, you can do that via PHP header().您不想在点击时重定向,您可以通过 PHP header() 来实现。

header('Location: http://pvpcity.dx.am/submitted/index.html');

This will redirect after submit the form...这将在提交表单后重定向...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM