Codeigniter笑脸没有出现在预览区域

时间:2016-10-31 06:34:46

标签: javascript jquery codeigniter

我创建了一个区域,用户可以在其中键入一些文本,并在文本区域添加笑脸。笑脸helper

当我点击我的预览按钮时,它不会显示生成的笑脸。

enter image description here

  

问题如何在预览中确定是否有任何微笑   问题将在预览中正确显示。我在控制器上的预览功能上使用了preg_replace_callback。还有.replace脚本更新我在用户指南上找到this但不知道在代码上添加它的位置

在这里产生微笑。

<?php

class Question extends CI_Controller {

    public $data = array();

    public function __construct() {
        parent::__construct();
        $this->load->helper('smiley');
    }

    public function create() {

        $this->load->library('table');

        $image_array = get_clickable_smileys(base_url('assets/img/smiley'), 'question');

        $col_array = $this->table->make_columns($image_array, 8);

        $data['smileys'] = $this->table->generate($col_array);

        $this->form_validation->set_rules('title', 'title', 'trim|required');
        $this->form_validation->set_rules('question', 'question', 'trim|required|callback_question');

        if ($this->form_validation->run() == true) {

        }

        $data['page'] = 'question/create';

        $this->load->view($this->config->item('config_template') . '/template/template_view', $data);
    }

    public function preview() {
        $data = array('success' => false, 'question' => '', 'tag' => '');

        if ($_POST) {
            $string = $this->input->post('question');

        $match = array(
            '<' => '&lt;',
            '>' => '&gt;',
        );

        $new_data = preg_replace_callback("#</?(pre|code|h1|h2|h3|h4|h5|h6|b|strong|i|u|hr)>|[<>]#", function ($match) {
        return $match[0] == '<' ? '&lt;' : ($match[0] == '>' ? '&gt;' : $match[0]);
        }, $string);

            $data['question'] =  $new_data;
            $data['success'] = true;
        }

        $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode($data));
    }


}

观看脚本

<script type="text/javascript">
$('#preview-question').on('click', function (e) {
    $.ajax({
        url: "<?php echo base_url('question/preview');?>",
        type: 'POST',
        data: {
            question: $('#question').val(),
            '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
        },
        dataType: 'json',
        success: function(response){
            if (response.success) {
                $('#preview').html(response.question);
                if ($("#preview").find("pre").length > 0){  
                    var html = $('#preview pre').html().replace(/</g, "&lt;").replace(/>/g, "&gt;"); 
                    $('#preview pre').html(html);  
                    $('pre').each(function(i, block) {
                        hljs.highlightBlock(block); 
                    });
                }
            } else {

            }
        }

    });

    e.preventDefault();  
});

function wrapText(elementID, openTag, closeTag) {
    var textArea = $('#' + elementID);
    var len = textArea.val().length;
    var start = textArea[0].selectionStart;
    var end = textArea[0].selectionEnd;
    var selectedText = textArea.val().substring(start, end);
    var replacement = openTag + selectedText + closeTag;
    textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len));
} 

$(document).ready(function(){
    $('#bold').click(function() {
        wrapText('question', "<strong>", "</strong>");
    });
    $('#italic').click(function() {
        wrapText("question", "<i>", "</i>");
    });
    $('#underline').click(function() {
        wrapText("question", "<u>", "</u>");
    });
    $('#pre').click(function() {
        wrapText("question", "<pre>", "</pre>");
    });
});
</script>

1 个答案:

答案 0 :(得分:0)

搞定了

我必须使用$data['question'] = parse_smileys($new_data, base_url('assets/img/smiley'));

public function preview() {
    $data = array('success' => false, 'question' => '', 'tag' => '');

    if ($_POST) {
        $string = $this->input->post('question');

        $match = array(
            '<' => '&lt;',
            '>' => '&gt;',
        );

        $new_data = preg_replace_callback("#</?(pre|code|h1|h2|h3|h4|h5|h6|b|strong|i|u|hr)>|[<>]#", function ($match) {
        return $match[0] == '<' ? '&lt;' : ($match[0] == '>' ? '&gt;' : $match[0]);
        }, $string);

        $data['question'] = parse_smileys($new_data, base_url('assets/img/smiley'));
        $data['success'] = true;
    }

    $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode($data));
}

enter image description here