简体   繁体   English

PHP中的文件创建从javascript获取内容

[英]File creation in PHP getting content from javascript

I am trying to make a website where the user is supposed to enter a short text in a textarea aswell as entering their e-mail and a personal id. 我正在尝试建立一个网站,在该网站中,用户应该在文本区域中输入短文本以及输入他们的电子邮件和个人ID。 I am trying to send this through javascript in one variable. 我正在尝试通过javascript将其发送到一个变量中。 Problem is when Irun the code no file is created. 问题是当我运行代码时没有创建文件。 Here is the code I am using. 这是我正在使用的代码。

    function load(){
       var xmlhttp;
       if(window.XMLHttpRequest){
              xmlHttp= new XMLHttpRequest();
           }else{
       xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}


    var textToSend=document.forms["form"].id.value + " "+document.forms["form"].mail.value+" "+document.forms["form"

xmlhttp.open("GET","serverside.php?txt="+textToSend,"true");
xmlhttp.send();
   }



    <?php
    $txt=$_GET["txt"];
    $spacing="\n \n";

    $my_file = 'userdata.txt';
    $handle = fopen($my_file, 'a') or die('Cannot open file: '.$my_file);
    fwrite($handle, $txt);
    fclose($handle);

    ?>

I cannot guarantee any of these will fix the problem, but you should try them: 我不能保证这些方法都能解决问题,但您应该尝试以下方法:
1) using POST instead of GET 1)使用POST而不是GET
2) instead of $my_file='userdata.txt' try $my_file='./userdata.txt' 2)代替$my_file='userdata.txt'尝试$my_file='./userdata.txt'
3) having w instead of a in fopen 3)在fopen w代替a

The fopen function does not create files by default -- it fails with "No such file or directory" error. 默认情况下, fopen函数不会创建文件-它将失败,并显示“无此文件或目录”错误。 You say that "no file is created", so I believe you didn't create the userdata.txt file manually. 您说“未创建文件”,所以我相信您不是手动创建userdata.txt文件。

Create empty userdata.txt file and see if that helps. 创建一个空的userdata.txt文件,看看是否有帮助。

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

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