简体   繁体   English

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

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

I have a trouble in getting the value of CKEDITOR in HTML.我在 HTML 中获取 CKEDITOR 的值时遇到了麻烦。 After inputting some HTML tags in the CKEDITOR and submitting it.在CKEDITOR中输入一些HTML标签并提交后。 There's no output from the POST response. POST 响应没有输出。 Here's my code.这是我的代码。 I don't know if it some complication because i also used jquery-validation plugin.我不知道它是否有些复杂,因为我还使用了 jquery-validation 插件。

HTML 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 JS

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

jquery-validation 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 server side PHP服务器端

public function sendEmail() {

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

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

Can you help me with this?你能帮我解决这个问题吗?

Try this:试试这个:

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

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

Or或者

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

Reference Guide参考指南

Thanks for the help guys, I manage to solve it by downloading this small plugin http://ckeditor.com/addon/save感谢您的帮助,我设法通过下载这个小插件http://ckeditor.com/addon/save来解决它

And I add the configuration in my js part.我在我的 js 部分添加了配置。

config.extraPlugins = 'save';

And now I can get the CKEDITOR value.现在我可以获得 CKEDITOR 值。

你能试试这个方法吗

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

Just try this and it works fine试试这个,它工作正常

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

You will get all the form's fields & their values and confirm with the network in the google console.您将获得表单的所有字段及其值,并在 google 控制台中与网络确认。 like:像:

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

we are now getting our texteditor value in the editor-key above.我们现在在上面的编辑器键中获取我们的 texteditor 值。

On the PHP page you can just get the values like:在 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

The Mayank answer was right, but i've made a most flexible approach for this using "for in" syntax, you can make the following on your submitHandler, just before send the data: 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
}

And you got the correctly data on $_POST你在 $_POST 上得到了正确的数据

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

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