3

我正在制作一个http://c9.io 之类的服务来在浏览器上编辑我的服务器上的 .php 文件。

我在那里实现了 CodeMirror 编辑器。编辑器如下所示:http ://codemirror.net/mode/php/index.html

我的问题是我无法通过 jQuery.ajax POST 发送带有 php 代码的数据。

假设我想将以下几行保存到 hello.php:

 <?php 
         require_once("lukujA.php"); 
 ?>

我正在使用以下 js / jquery 代码来保存文件:

$(".save-file").click(function (){
            var content = editor.getValue(); //textarea text
            var path = "hello.php";
        //following line shows content-data as it shows on CodeMirror editor
        //confirm box without any quotes / slashes / and with every linebreak

            var response = confirm("Do you want to save? DATA: " + content);
            if(response)
            {
                $.ajax({
                    type: "GET",
                    url: "saveFile.php",
                    data: "content="+content+"&path="+path+"",
                    success: function(){
                        alert("File saved!"); 
                    }
                });
            }
            else{
                alert("File not saved!");
            }
        });

保存文件.php:

$path = $_GET['path'];
$content = $_GET['content'];

if($path !== "" and is_writable($path))
    file_put_contents($path, $content);

上面的代码输出 hello.php 如下所示(在一行上并带有斜杠)(使用 POST 似乎删除了我在编辑器上所做的任何换行符):

<?php require_once(\"lukujA.php\"); ?>

如果我有 php 代码,我不能stripslashes($content);在 saveFile.php 上使用:

<?php echo "<input type=\"text\" name=\"foo\">"; ?>

strip_slashes将删除这些斜杠,并且代码在执行时将变得无效。

我应该如何遇到这个问题,我应该如何将新代码保存到文件中?你会怎么做这样的编辑器?

谢谢

4

1 回答 1

3

得到了整个工作与以下:

    $(".save-file").click(function (){
        editor.save();
        var content = editor.getValue(); //textarea text
        var path = $("#hiddenFilePath").text(); //path of the file to save
        var response = confirm("Do you want to save?");
        if(response)
        {
            $.ajax({
                type: "POST",
                url: "saveFile.php",
                data: {c:content,p:path},
                dataType: 'text',
                success: function(){
                    alert("File saved!"); 
                }
            });
        }
        else{
            alert("File not saved!");
        }
    });

查看代码data: {c:content,p:path}, dataType: 'text',

在 saveFile.php 中,我使用stripslashes($content)了因为似乎我在 php 设置上有魔术引号。

当我需要发送像echo "<br><a href=\"?p=$p&vko=$vkoPrev\">Edellinen</a> <a href=\"?p=$p&vko=$vko\">Tämä viikko</a> <a href=\"?p=$p&vko=$vkoNext\">Seuraava</a><br><br>";stripslashes 这样的数据时,仍然会在我的数据引号之前保留这些斜杠,因为当我通过 POST 发送数据时,就像上面看到的 urlEncoding 在我的数据斜杠之前添加斜杠。

难以解释。希望将来有人能从中得到一些东西:) 对不起,英语也不是很好。

于 2012-03-03T11:41:00.537 回答