6

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 however I cannot find anyway to submit a users input to the database. I can see there is a textarea generated by the JavaScript however I'm not exactly sure what it is doing, how to get to it or if I should be looking for something else entirely.

I'm primarily looking for a field or something that I can use to have php submit into the db.

4

1 回答 1

14

编辑窗口的内容可通过会话 getValue 方法获得。例如,这里是保存文件的标准 ACE 演示的扩展:

saveFile = function() {
    var contents = env.editor.getSession().getValue();

    $.post("write.php", 
            {contents: contents },
            function() {
                    // add error checking
                    alert('successful save');
            }
    );
};

我将 saveFile 调用添加到 demo.js 中已经存在的“Fake Save”。我用这样的代码替换警报:

// 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();
    }
});

php 文件只有一行:

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

于 2011-07-12T16:35:04.963 回答