我在使用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-validation
$('#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
你能帮我解决这个问题吗?
答案 0 :(得分:4)
试试这个:
<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
?>
答案 1 :(得分:1)
感谢帮助人员, 我设法通过下载这个小插件来解决它 http://ckeditor.com/addon/save
我在js部分添加配置。
config.extraPlugins = 'save';
现在我可以获得CKEDITOR值了。
答案 2 :(得分:0)
你可以尝试这种方法
$details = htmlentities($_POST['editor']);
答案 3 :(得分:0)
只需尝试一下,它就可以正常工作
$("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 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
答案 4 :(得分:0)
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上获得了正确的数据