V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wdc63
V2EX  ›  PHP

请教 PHP 问题, PHP 读取 html 文件为变量,将其赋值给 js 总是失败

  •  
  •   wdc63 · 2017-10-10 16:41:23 +08:00 · 2506 次点击
    这是一个创建于 2382 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我是完全不会 PHP,为了解决一个读取 html 文件到可视化编辑器中的问题,在网上搜索,自己尝试了几个小时都没找到有效的办法,因此求助

    http://gist.github.com/wdc63/358cab5f0d1905c43a28c0aa7d9aa5a0

    以上运行总是失败,无法 alert 任何东西 html 文件示例: http://123.206.114.168:15679/test.html

    10 条回复    2017-10-10 20:21:19 +08:00
    cxbig
        1
    cxbig  
       2017-10-10 16:50:26 +08:00 via iPad
    先把变量用 var_dump 出来看看
    另外直接输出 html 文本显然会有大量的单双引号,这会破坏 Javascrip 结构
    smallyin
        2
    smallyin  
       2017-10-10 16:51:13 +08:00
    两个问题:
    1 ) php 文件第 4 行中括号是多余的
    2 ) var gethtml = "<?php echo $htmlData ;?>"; 改成 var gethtml = `<?php echo $htmlData ;?>`;
    cxbig
        3
    cxbig  
       2017-10-10 16:51:33 +08:00 via iPad
    更正:Javascript。手残,漏了个字母。
    wdc63
        4
    wdc63  
    OP
       2017-10-10 17:04:05 +08:00
    @smallyin 谢谢,我先试试
    wdc63
        5
    wdc63  
    OP
       2017-10-10 17:04:36 +08:00
    @cxbig
    @smallyin
    没有 php 编程环境,都是在服务器上直接改 php 文件然后远程访问看有问题没
    wdc63
        6
    wdc63  
    OP
       2017-10-10 17:09:51 +08:00
    @smallyin 改之后还是不行
    cxbig
        7
    cxbig  
       2017-10-10 17:12:53 +08:00 via iPad
    @wdc63 var_dump 可以在任何环境使用。你先确定远程可以执行 PHP ?
    niuoh
        8
    niuoh  
       2017-10-10 17:13:05 +08:00
    php 赋值给 js 的时候 html encode
    js 使用的时候再 html decode
    smallyin
        9
    smallyin  
       2017-10-10 17:13:39 +08:00
    什么浏览器?我在 chrome 下亲测可以的呀
    wdc63
        10
    wdc63  
    OP
       2017-10-10 20:21:19 +08:00
    @cxbig 谢谢,解决了,确实是浏览器的问题。
    @smallyin
    @niuoh
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5463 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 03:08 · PVG 11:08 · LAX 20:08 · JFK 23:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.