drvfqr5609 2017-10-24 09:01
浏览 73
已采纳

将多行PHP字符串插入Javascript变量

My question
My question is about correctly parsing HTML from php into a variable inside of a javascript object (which in this case is ace-editor's value variable)

My problem
I have got a textarea for HTML and CSS, the HTML gets retrieved from the database and needs to be inserted into a field of croppie, i am currently using PHP's json_encode functionality to put it inside of the variable, but it seems to still escape from the value.

My code

<?php

        $css = ($modify) ? trim($template->style()) : "";
        $html = ($modify) ? trim( $template->html() ) : "";

        $html = json_encode($html);
        $css = json_encode($css);


    ?>
YUI().use(
        'aui-ace-editor',
        function(Y) {
            var editorhtml = new Y.AceEditor({
                boundingBox: '#editor-html',
                height: '400',
                mode: 'html',
                value: '<?php echo substr( $html, 1, -1 ); ?>',
                width: '100%',
                showPrintMargin: false
            }).render();

            var editorcss = new Y.AceEditor({
                boundingBox: '#editor-css',
                height: '400',
                mode: 'css',
                value: '<?php echo substr( $css, 1, -1 ); ?>',
                width: '100%',
                showPrintMargin: false
            }).render();
        }
    );

What happens
When i use this, and open up a specific template which can be managed, i will not be able to see the textarea's (because the ace editor could not be loaded), i get random errors relating to line 666 which is the exact line the HTML gets stored in. I did sanitize the outputs in json_encode correctly.. right?

Screenshot of html inserted into page
in this screenshot, you can see the HTML/css which gets inserted. But the problem occurs on line 666 which the HTML is located at.

Click here if the screenshot isn't readable for you

So my question is..
Why exactly does it not parse the HTML into the object correctly? Am i not sanitizing it correctly or am i missing something?

  • 写回答

2条回答 默认 最新

  • dox19458 2017-10-24 09:12
    关注

    Your issue is because your $html string contains single quotes, try escaping them or using double quotes

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择