0

我目前正在尝试将其保存在文件中。但它不会工作。

有谁知道我做错了什么?当我按下verturen按钮时我的目标是保存值, rog文本文件中,然后将其保存在$s1array. 之后,如果我按下载,他会将其加载$s1array到文本文件中。我想$s1array在剩下的时间里保持新的价值。

(这是我的代码)

    <form method="post">
    <input type="submit" name="submit1" value="Versturen">
    <select name="state1sl1">
      <option name="groen" value="g">groen</option>}
      <option name="oranje" value="o">oranje</option>
      <option name="rood" value="r">rood</option>}
    </select>

    <select name="state1sl2">
      <option name="groen" value="g">groen</option>}
      <option name="oranje" value="o">oranje</option>
      <option name="rood" value="r">rood</option>}
    </select>

    <select name="state1sl3">
      <option name="groen" value="g">groen</option>}
      <option name="oranje" value="o">oranje</option>
      <option name="rood" value="r">rood</option>}
    </select>
    </form>

<form method="post">
<input type="submit" name="download" value="download">
</form>


    <?php
    if(isset($_POST['submit1'])){
    $_name1 = $_POST['state1sl1'];
    $_name2 = $_POST['state1sl2'];
    $_name3 = $_POST['state1sl3'];
    $my_File = 'T-Splitsing.txt';
    $fh = fopen($my_File, 'w') or die("can't open file");
    fwrite($fh, $_name1);
    fwrite($fh, $_name2);
    fwrite($fh, $_name3);
    fclose($fh);
    $s1array = file_get_contents("T-Splitsing.txt");
    }

  if(isset($_POST['download'])){


   $file = 'T-Splitsing.txt';

     if (file_exists($file)) {
     $my_File = 'T-Splitsing.txt';
     $fh = fopen($my_File, 'w') or die("can't open file");
     fwrite($fh, $s1array);
     fclose($fh);

    ?>
4

1 回答 1

0

You can't do that, since PHP does not remember state. Each time you submit a form, this is a new HTTP request. Nevertheless, what you could do (as I see that you are saving the data into "T-Splitsing.txt") is to simply load the data from the file and send it to the user. The "download" part of the code would become:

 if(isset($_POST["download"])){
   $file = "T-Splitsing.txt";
   header("Content-Type: text/plain");
   header("Content-disposition: attachment; filename=\"".$file."\""); 
   readfile($file);
 }
于 2013-04-08T09:45:58.513 回答