简体   繁体   English

PHP fopen()和fwrite()无法正常工作?

[英]PHP fopen() and fwrite() dont work?

I wanted a php script to execute when the user press the button. 我希望用户按下按钮时执行php脚本。 In my html file i used a jquery script 在我的html文件中,我使用了一个jquery脚本

<script>
        $(document).ready(function(){
            $(".btn").click(function(){

                var firstname = $("#firstname").val();
                var lasname = $("#lastname").val();
                var dataNames = { 'firstnameVal' : firstname};
                $.post("SaveNames.php",dataNames,function()
                       {
                    alert("hi");
                });
            });   
        });                
    </script>

and

<input type="text" id="firstname" name="firstname">
<input type="text" id="lastname" name="lastname">

in my php file i used some help from here 在我的PHP文件中,我从这里使用了一些帮助

<?php

if (isset($_POST["fistname"]))
{
        $firstname = $_POST["firstname"];
        $SavedNames = fopen("Names.txt","w");

        $NamesText = "Its Work!";

        fwrite($SavedNames,$NamesText);
        fclose($SavedNames);
}
?>

The problem is the fopen() doesn't work (don't create file) and I tried to create the file to check if fwrite() works but it's also didn't worked. 问题是fopen()不起作用(不创建文件),我试图创建文件来检查fwrite()起作用,但它也没有起作用。

I use localhost wamp server. 我使用本地主机服务器。

Try to set the complete path to your file: 尝试设置文件的完整路径:

fopen("c:\\folder\\Names.txt", "w");

Make sure file permissions are set correctly (writeable) to c:\\\\folder\\\\ . 确保将文件权限正确设置(可写)到c:\\\\folder\\\\ Pay attention to lower-/uppercase naming. 注意小写/大写命名。

Try if this works: 尝试这样做是否可行:

if (isset($_POST["firstname"]))
{
        $firstname = $_POST["firstname"];
        if(file_put_contents("Names.txt", $firstname) !== false)
            echo "It works!\n";
}

Firstly, isset($_POST["fistname"]) , you mispelled "firstname". 首先, isset($_POST["fistname"]) ,您拼写了“ firstname”。

Secondly, on the php side you use $_POST['firstname'] . 其次,在php端,您使用$_POST['firstname'] But in the javascript, you have 但是在javascript中,

var dataNames = { 'firstnameVal' : firstname};

Try it with 尝试一下

var dataNames = { 'firstname' : firstname};

As saided, try checking if the folder in which you are trying to write exist and have privileges to write in. But first clear your mispelling errors and variable names. 如前所述,请尝试检查您要写入的文件夹是否存在并具有写入权限。但是首先要清除拼写错误和变量名。 Code below will post value with name which you declared in brackets. 下面的代码将发布带有您在方括号中声明的名称的值。 So in your example firstnameVal. 因此,在您的示例firstnameVal中。 So you need to check if this POST value exist. 因此,您需要检查此POST值是否存在。

var dataNames = { 'firstnameVal' : firstname};

If this didn't work use php function var_dump['variableName']; 如果这不起作用,请使用php函数var_dump ['variableName']; to display data after post method. 在发布方法之后显示数据。 So you can get handle on data flow in your files. 因此,您可以处理文件中的数据流。

问题是从wampserver重新安装的,它确实起作用了

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

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