简体   繁体   English

如何将函数的结果保存到txt文件中?

[英]How do I save the results of my function to a txt file?

I am trying to take data from my quiz and enter it into a txt file using php. 我正在尝试从测验中获取数据,然后使用php将其输入到txt文件中。 This is my quiz code: 这是我的测验代码:

 function check(){
    var question1=document.quiz.question1.value;
      if (question1=="Yes"){
            correct++;
      }
    document.getElementById("number_correct").innerHTML="Score "+correct+" /10";
    }

I need to enter the value of number_correct into a txt document once a button is clicked on my html doc. 单击我的html文档上的按钮后,我需要在txt文档中输入number_correct的值。 Heres my PHP: 这是我的PHP:

    <?php

    if(isset($_POST['number_correct']))
{
$data=$_POST['number_correct'];
$fp = fopen('results.txt', 'a');
fwrite($fp, $data);
fclose($fp);
}
?>

Heres my button: 这是我的按钮:

<div class="wrapper">
    <form class="frm1" method="post">
       <input class="form-control" id="button" type="button"  value="Submit" onclick="check();"><br>

      <p methods="post" id="number_correct" name="number_correct">
  </p></form><
   </div>

My issue is that once the button is clicked and the value of number_correct is returned it is not written to reuslts.txt. 我的问题是,一旦单击按钮并返回number_correct的值,它就不会写入reuslts.txt。

  let download = (stringContents, fileName) => { const blob = new Blob([stringContents], {type: 'application/json'}); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = fileName; a.click(); }; let results = 'Congratulations! You got 9 questions right... out of 150 :('; download(results, 'results.txt'); 

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

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