Get and save content

You can save text from Redactor using one of four different approaches.

Sending form data using POST method #

<form action="" method="post">
    <textarea id="content" name="content"></textarea>
    <button>Send</button>
</form>

<!-- call -->
<script>
$R('#content');
</script>

In this example, text from Redactor will be sent as a form when user clicks on Send button. On a server side you’ll receive a variable POST with a name content.

Form serialize and AJAX #

<!--form -->
<form action="/savedata-server-script/" id="my-form" method="post">

    <!-- element -->
    <textarea id="content" name="content"></textarea>

    <!-- submit button -->
    <button onclick="sendForm();">Send</button>
</form>

<!-- call -->
<script>
$R('#content');
</script>

<!-- send function with jQuery -->
<script>
function sendForm()
{
    $.ajax({
        url: '/savedata-server-script/',
        type: 'post',
        data: $('#my-form').serialize()
    });
}
</script>

Redactor API method #

<!--form -->
<form action="/server-script/" method="post">

    <!-- element -->
    <textarea id="content" name="content"></textarea>

    <!-- submit button -->
    <button onclick="sendForm();">Send</button>
</form>

<!-- call -->
<script>
$R('#content');
</script>

<!-- get and send content with API -->
<script>
function sendForm()
{
    var content = $R('#content', 'source.getCode');

    $.ajax({
        url: '/savedata-server-script/',
        type: 'post',
        data: 'content=' + content
    });
}
</script>

Autosave #

If you wish the Redactor to automatically save the text with each change, just set the path to a script that will handle the incoming data.

<!--element -->
<textarea id="content"></textarea>

<!-- call -->
<script>
$R('#content', {
    autosave: '/your-autosave-server-script/',
    callbacks: {
        autosave: function(name, data, response)
        {
            // name - name of textarea or the value of 'autosaveName' option
            // data - an object with the data sent to the server
            // response -  the server JSON response
        }
    }
});
</script>

In this examples, the Redactor will transmit data to the save.php every two minutes (in background mode). Autosave will transmit the POST textareaname variable.

You can use autosaveError callback for a triggering when request for an autosave has been error.

$R('#content', {
    callbacks: {
        autosaveError: function(name, data, response)
        {
            // ...
        }
    }
});

Your server-side script must pass error as JSON and it must contain the error parameter, for example:

{
    "error": true,
    "message": "Something went wrong..."
}