CKeditor 数据在 PHP 验证 Codeigniter 中无法识别
CKedit data not recognize in PHP validation Codeigniter
首先,我不知道t really now how to explain this but its sounds complicated to me. It makes my brain broken and I really don
是否有人可以帮助我解决我的 CKeditor 问题。
我的控制器代码:
public function reply_topic()
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('comment', 'Comment Field Required', 'required');
//$this->form_validation->set_rules('g-recaptcha-response', 'recaptcha validation', 'required|callback_validate_captcha');
//$this->form_validation->set_message('validate_captcha', 'Please check the the captcha form');
if ($this->form_validation->run() == FALSE)
{
$this->load_reply_to_topic_view($this->input->post('topic_id'));
}
else
{
$topic_id = $this->input->post('topic_id');
$this->topic_model->postReplyTopic();
echo '<div class="alert alert-success">'.lang_key('reply_submited').'</div>';
$this->load_reply_to_topic_view($topic_id='');
}
}
此代码没有问题,开始工作正常。但是当我尝试提交数据时,它总是会出现错误。
就像 评论字段为空 即使我已经将数据归因于评论部分 - 如果我使用 CKeditor,则会出现此错误。但是,如果我只使用普通的文本区域,它确实可以正常工作。
<!-- Javascript -->
<script type="text/javascript">
CKEDITOR.replace( 'comment' );
</script>
这里是评论部分出现的代码
<script type="text/javascript">
var loadUrl = '<?php echo base_url("threads/load_reply_to_topic_view/".$item['id']);?>';
jQuery.post(
loadUrl,
{},
function(responseText){
jQuery('.reply-topic-form-holder').html(responseText);
init_reply_topic_js();
}
);
function init_reply_topic_js()
{
jQuery('#replytopic-form').submit(function(e){
var data = jQuery(this).serializeArray();
jQuery('.topic-loading').show();
e.preventDefault();
var loadUrl = jQuery(this).attr('action');
jQuery.post(
loadUrl,
data,
function(responseText){
jQuery('.reply-topic-form-holder').html(responseText);
jQuery('.alert-success').each(function(){
jQuery('#replytopic-form textarea').each(function(){
jQuery(this).val('');
});
});
jQuery('.topic-loading').hide();
init_reply_topic_js();
}
);
});
}
</script>
CK 实际上并没有使用 textarea 字段,可以说它只是一个 'placeholder'。因此,您必须确保将 CK 中的数据添加到文本区域,否则当您通过 AJAX post 时,您将得不到该字段的数据。当 post 正常时,这不是问题,因为 autoUpdateElement。
在 var data = jQuery(this).serializeArray();
之前添加 CKEDITOR.instances.comment.updateElement();
。这将确保 CK 编辑器的内部标记被添加到文本区域,因此它将在序列化数组之前填充该字段。
关于方法的文档 here。
jQuery('#replytopic-form').submit(function (e) {
CKEDITOR.instances.comment.updateElement();
var data = jQuery(this).serializeArray();
jQuery('.topic-loading').show();
e.preventDefault();
var loadUrl = jQuery(this).attr('action');
jQuery.post(
loadUrl,
data,
function (responseText) {
jQuery('.reply-topic-form-holder').html(responseText);
jQuery('.alert-success').each(function () {
jQuery('#replytopic-form textarea').each(function () {
jQuery(this).val('');
});
});
jQuery('.topic-loading').hide();
init_reply_topic_js();
}
);
});
首先,我不知道t really now how to explain this but its sounds complicated to me. It makes my brain broken and I really don
是否有人可以帮助我解决我的 CKeditor 问题。
我的控制器代码:
public function reply_topic()
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('comment', 'Comment Field Required', 'required');
//$this->form_validation->set_rules('g-recaptcha-response', 'recaptcha validation', 'required|callback_validate_captcha');
//$this->form_validation->set_message('validate_captcha', 'Please check the the captcha form');
if ($this->form_validation->run() == FALSE)
{
$this->load_reply_to_topic_view($this->input->post('topic_id'));
}
else
{
$topic_id = $this->input->post('topic_id');
$this->topic_model->postReplyTopic();
echo '<div class="alert alert-success">'.lang_key('reply_submited').'</div>';
$this->load_reply_to_topic_view($topic_id='');
}
}
此代码没有问题,开始工作正常。但是当我尝试提交数据时,它总是会出现错误。 就像 评论字段为空 即使我已经将数据归因于评论部分 - 如果我使用 CKeditor,则会出现此错误。但是,如果我只使用普通的文本区域,它确实可以正常工作。
<!-- Javascript -->
<script type="text/javascript">
CKEDITOR.replace( 'comment' );
</script>
这里是评论部分出现的代码
<script type="text/javascript">
var loadUrl = '<?php echo base_url("threads/load_reply_to_topic_view/".$item['id']);?>';
jQuery.post(
loadUrl,
{},
function(responseText){
jQuery('.reply-topic-form-holder').html(responseText);
init_reply_topic_js();
}
);
function init_reply_topic_js()
{
jQuery('#replytopic-form').submit(function(e){
var data = jQuery(this).serializeArray();
jQuery('.topic-loading').show();
e.preventDefault();
var loadUrl = jQuery(this).attr('action');
jQuery.post(
loadUrl,
data,
function(responseText){
jQuery('.reply-topic-form-holder').html(responseText);
jQuery('.alert-success').each(function(){
jQuery('#replytopic-form textarea').each(function(){
jQuery(this).val('');
});
});
jQuery('.topic-loading').hide();
init_reply_topic_js();
}
);
});
}
</script>
CK 实际上并没有使用 textarea 字段,可以说它只是一个 'placeholder'。因此,您必须确保将 CK 中的数据添加到文本区域,否则当您通过 AJAX post 时,您将得不到该字段的数据。当 post 正常时,这不是问题,因为 autoUpdateElement。
在 var data = jQuery(this).serializeArray();
之前添加 CKEDITOR.instances.comment.updateElement();
。这将确保 CK 编辑器的内部标记被添加到文本区域,因此它将在序列化数组之前填充该字段。
关于方法的文档 here。
jQuery('#replytopic-form').submit(function (e) {
CKEDITOR.instances.comment.updateElement();
var data = jQuery(this).serializeArray();
jQuery('.topic-loading').show();
e.preventDefault();
var loadUrl = jQuery(this).attr('action');
jQuery.post(
loadUrl,
data,
function (responseText) {
jQuery('.reply-topic-form-holder').html(responseText);
jQuery('.alert-success').each(function () {
jQuery('#replytopic-form textarea').each(function () {
jQuery(this).val('');
});
});
jQuery('.topic-loading').hide();
init_reply_topic_js();
}
);
});