繁体   English   中英

如何在 PHP 中使用 POST 从 CKEDITOR 获取值?

[英]How can I get the values from CKEDITOR using POST in PHP?

我在 HTML 中获取 CKEDITOR 的值时遇到了麻烦。 在CKEDITOR中输入一些HTML标签并提交后。 POST 响应没有输出。 这是我的代码。 我不知道它是否有些复杂,因为我还使用了 jquery-validation 插件。

HTML

<?php echo form_open('users/user/sendEmail', array('id' => 'send-mail-form', 'role' => 'form')); ?>
<div class="row row_field">
    <div class="col-md-12">
        <label for="editor">Email Body:</label>
        <textarea name="email_body" id="editor"></textarea>
    </div>
</div>

JS

$('#editor').ckeditor();

jquery-验证

$('#send-mail-form').validate({
    rules: {
        email_subject: {
            required: true,
            minlength: 15
        },
        email_body: {
            required: true,
            minlength: 50
        }
    },
    messages: {
        email_subject: {
            required: "The Email Subject is required",
            minlength: "The email subject shoud be 15 characters and above."
        },
        email_body: {
            required: "Email body is required",
            minlength: "The email body should be 5o characters and above."
        }
    },
    submitHandler: function(form) {
        form.submit(); //send data
    }
});

PHP服务器端

public function sendEmail() {

        fp($this->input->post()); //no output 

        fp(htmlentities($this->input->post('email_body'))); //no output also

你能帮我解决这个问题吗?

试试这个:

<script>
    var data = CKEDITOR.instances.editor1.getData();

    // Your code to save "data", usually through Ajax.
</script>

或者

<?php
    $editor_data = $_POST[ 'editor1' ];   // where editor1 is the name of html element
?>

参考指南

感谢您的帮助,我设法通过下载这个小插件http://ckeditor.com/addon/save来解决它

我在我的 js 部分添加了配置。

config.extraPlugins = 'save';

现在我可以获得 CKEDITOR 值。

你能试试这个方法吗

$details =  htmlentities($_POST['editor']);

试试这个,它工作正常

 $("form[name='blog_form']").on("submit", function (e) {
    e.preventDefault();
    var editor_data = CKEDITOR.instances.editor.getData();  // editor is the textarea field's name
    $("form[name='blog_form'] textarea[name='editor']").val(editor_data);
    var data = new FormData(this);

您将获得表单的所有字段及其值,并在 google 控制台中与网络确认。 像:

b_title: This is just a testing Title of the blog.
b_heading: This is the Heading of the Blog.
b_date: 2019-09-06
b_time: 17:00
b_excerpt: test
editor: <p>testing <strong>may be fine </strong>now with&nbsp; the <span 
style="color:#1abc9c"><span style="font-size:24px">TextEditor</span></span></p>
b_file: (binary)
b_tags: tags

我们现在在上面的编辑器键中获取我们的 texteditor 值。

在 PHP 页面上,您可以获得如下值:

$blog = new Blog($db);

$blog->b_title = $_POST['b_title'];
$blog->b_heading = $_POST['b_heading'];
$blog->b_content = $_POST['editor'];  // ... and so on

Mayank 的答案是正确的,但我使用“for in”语法为此做了一个最灵活的方法,您可以在发送数据之前在 submitHandler 上进行以下操作:

submitHandler: function(form) {
    //Update textarea elements before send...
    for (let input in CKEDITOR.instances) {
        CKEDITOR.instances[input].updateElement();
    }

    form.submit(); //send data
}

你在 $_POST 上得到了正确的数据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM