Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ace Editor in PHP Web App

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.

like image 456
Dave Bergschneider Avatar asked Jul 12 '11 04:07

Dave Bergschneider


People also ask

What is ACE code editor?

Ace is an embeddable code editor written in JavaScript. It matches the features and performance of native editors such as Sublime, Vim and TextMate. It can be easily embedded in any web page and JavaScript application.

How do I get an ace editor value?

To get the value from ace editor, use getValue() method as below. var myCode = editor. getSession(). getValue();


1 Answers

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");

like image 138
Paul Beusterien Avatar answered Sep 18 '22 23:09

Paul Beusterien