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条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题