简体   繁体   中英

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. 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. Heres my 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.

  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'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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