Ace Editor in PHP Web App

前端 未结 1 1220
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 11:06

I am making a small web app that allows users to submit html, css and javascript content via Ace Editor. In this editor, echoing stored content into the editor is simply enough

相关标签:
1条回答
  • 2021-02-06 12:06

    The contents of the edit window are available with the session getValue method. For example, here is an extension to the standard ACE demo for save file:

    saveFile = function() {
        var contents = env.editor.getSession().getValue();
    
        $.post("write.php", 
                {contents: contents },
                function() {
                        // add error checking
                        alert('successful save');
                }
        );
    };
    

    I added the saveFile call to the already existing "Fake Save" that is in demo.js. I replace the alert with code like this:

    // Fake-Save, works from the editor and the command line.
    canon.addCommand({
        name: "save",
        bindKey: {
            win: "Ctrl-S",
            mac: "Command-S",
            sender: "editor|cli"
        },
        exec: function() {
            saveFile();
        }
    });
    

    The php file is just one line:

    $r = file_put_contents("foo.txt", $_POST["contents"]) or die("can't open file");

    0 讨论(0)
提交回复
热议问题