PHP无法编辑.txt文件

时间:2020-03-24 06:26:34

标签: javascript php xmlhttprequest file-put-contents

我是使用PHP的新手。我用JavaScript(在html文件中)编写程序,并使用XMLHttpRequest()向php文件发送一些值,然后我希望此php文件覆盖txt文件中的内容,但它没有改变txt文件中的任何内容。我不知道为什么。

我更新了代码

main.html:

<html>
    <label class="switch">
       <input id="switch" onclick="on_off()" type="checkbox">
       <span class="slider round"></span>
    </label>
</html>

<script type="text/javascript">
    function on_off(){
       var switchStatus = "false";
       var xmlhttp=new XMLHttpRequest();
       if ($('#switch').is(':checked')) {
          switchStatus = "true";
          xmlhttp.open("GET","../static/fwrite.php?q="+switchStatus,true);
          xmlhttp.send();
          console.log("save true")
       }
       else {
          switchStatus = "false";
          xmlhttp.open("GET","../static/fwrite.php?q="+switchStatus,true);
          xmlhttp.send();
          console.log("save false")
       }
    }
</script>

fwrite.php:

<?php

   $q=$_REQUEST["q"];
   file_put_contents("../static/temp.txt", $q);

?>

temp.txt :(默认为false。)

false

当我渲染该程序时,它没有错误,但是temp.txt并没有任何改变。 谁能告诉我该怎么解决?

0 个答案:

没有答案