如何从 php 中的 suneditor textarea 发送值?

How to send value from suneditor textarea in php?

我想在 php Suneditor

中发送文本区域值

PHP代码

if($_POST["submit"]){
    $content =  $_POST["txtEditor"];
    echo $content;
}

Html代码

 <form action="" method="post" >
        <textarea id="txtEditor" name="txtEditor" style="display:none;"></textarea>
        <input type="submit" name="submit" class="submit">
    </form>

Javascript

$(document).ready(function() {
    $("#txtEditor").Editor();
    $('form').submit(function () {
        $('#txtEditor').val($('#txtEditor').Editor('getText'));
    });
    $('#txtEditor').Editor('setText', $('#txtEditor').val());
});

suneditor.create('txtEditor', {
    buttonList: [
        ['undo', 'redo', 'removeFormat'],
        [align, font, fontSize, fontColor, hiliteColor],
        [horizontalRule, image, template]
    ],
})

编辑器工作完美。
我哪里做错了?

您必须将值从 suneditor 发送到 textarea,您可以这样做:

$(window).click(function() {
            document.getElementById('txtEditor').value = txtEditor.getContents();
        });

if条件中需要isset函数,否则会报错。

if(isset($_POST["submit"])){
    $content =  $_POST["txtEditor"];
    echo $content;
   }

无需为此编写任何 js 代码,只需将 text/content 放在 textarea 标记之间,就像:

<textarea id="txtEditor" name="txtEditor" style="display:none;">Here is your text</textarea>
OR
<textarea id="txtEditor" name="txtEditor" style="display:none;">
   <?= $content; ?>
</textarea>
// Gets the suneditor's context object. Contains settings, plugins, and cached element objects

editor.getContext();

// Gets the contents of the suneditor
// onlyContents {Boolean}: Return only the contents of the body without headers when the "fullPage" option is true

editor.getContents(onlyContents: Boolean);    

// Gets only the text of the suneditor contents

editor.getText();

// Change the contents of the suneditor
editor.setContents('set contents');

完整代码

const description = SUNEDITOR.create((document.getElementById('description')),{
            buttonList: [
                ['undo', 'redo'],
                ['bold', 'underline', 'italic'],
                ['removeFormat'],
                ['outdent', 'indent'],
                ['align', 'list'],
                ['table'],
                ['fullScreen', 'showBlocks']
            ],
            lang: SUNEDITOR_LANG['pl']
        });
        $(window).click(function() {
            document.getElementById('description').value = description.getContents();
        });

您只需要 运行 instance.save() 方法。

const instance = SUNEDITOR.create();
instance.save()