dousi7919 2011-09-19 17:15
浏览 70
已采纳

jQuery问题从PHP脚本接收html变量

I'm generating a bunch of tinymce edit boxes by sending it's content to javascript through php.

I'm doing something like

<script>
addBox('<?$content?>');
</script>

The problem is that everytime the text sent has a "/" character the function is broke an returns an error like:

Uncaught SyntaxError: Unexpected token ILLEGAL

I found it to return this error at least with this char... Don't know if it'll happen with others. The function is giving error when called like:

addBox("&lt;p&gt;Fundada em 2000 e inserida no &lt;strong&gt;Grupo CIL&lt;/strong&gt;, a CilNet &amp;eacute; uma empresa de Servi&amp;ccedil;os de Engenharia na &amp;aacute;rea das Tecnologias de Informa&amp;ccedil;&amp;atilde;o, com compet&amp;ecirc;ncias em Redes de Comunica&amp;ccedil;&amp;atilde;o de Dados, Voz e V&amp;iacute;deo.&lt;/p&gt;
&lt;p&gt;Tendo como base uma larga experi&amp;ecirc;ncia no mercado nacional, a CilNet assume-se como um parceiro tecnol&amp;oacute;gico no sector empresarial, com especializa&amp;ccedil;&amp;atilde;o em solu&amp;ccedil;&amp;otilde;es tecnol&amp;oacute;gicas pioneiras a n&amp;iacute;vel mundial.&amp;nbsp;&lt;/p&gt;");

Can anyone help?

The code for addBox is as follows:

function addBox(text){
    elem = "txt" + window.counter;

    var tiny = $.ajax({
        type: "POST",
        url: "inc/ajax.php?act=inserebox",
        data: "value=txt" + window.counter + "&text="+encodeURIComponent(text),
        async: false
    }).responseText;

    $('.more_boxes').append(tiny);
    //$(tiny).append('.more_boxes');


    tinyMCE.init({
       url:'../js/tinymce/jscripts/tiny_mce/plugins/ibrowser',
       mode:"exact",
       elements: elem,
       theme:"advanced",
       height:"220",
       entity_encoding : "raw",
       plugins : "safari,pagebreak,style,table,advimage,advlink,emotions,iespell,media,searchreplace,print,contextmenu,paste,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,inlinepopups,ibrowser",
       theme_advanced_toolbar_location : "top",
       theme_advanced_toolbar_align : "left",
       theme_advanced_statusbar_location : "bottom",
       theme_advanced_resizing : false
    });
    window.counter+=1;

    return true;
}
  • 写回答

1条回答 默认 最新

  • doupin8555 2011-09-19 17:20
    关注

    You can use PHP's built-in addslashes to escape illegal characters before they get passed to the tinymce box. You will need to do this to the $content var before passing it to the JS script.

    EDIT:

    Try a combination of decoded HTML and addSlashes like this:

    <?php
        // Code to create $content var here //
        $content = addSlashes($content);
    ?>
    
    <script>
        <![CDATA[
            addBox('<?php echo $content; ?>');
        ]]>
    </script>
    

    If you don't enclose your Javascript with <![CDATA[]]>, then you'll get errors if angle'd brackets are found, because it'll be interpreted as the start of an HTML tag.

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考