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

发布于 2024-12-05 09:11:46 字数 2212 浏览 0 评论 0原文

我通过 php 将其内容发送到 javascript 来生成一堆 tinymce 编辑框。

我正在做类似的事情

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

问题是每次发送的文本有一个“/”字符时,该函数就会被破坏并返回一个错误,例如:

Uncaught SyntaxError: Unexpected token ILLEGAL

我发现它至少用这个字符返回此错误...不知道是否这会发生在其他人身上。该函数在调用时出现错误,例如:

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;");

有人可以帮忙吗?

addBox的代码如下:

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;
}

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

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁拥 2024-12-12 09:11:46

您可以使用 PHP 的内置 addslashes 在非法字符传递到之前对其进行转义小盒子。在将 $content 变量传递给 JS 脚本之前,您需要对其执行此操作。

编辑:

尝试将解码的 HTML 和 addSlashes 组合起来,如下所示:

<?php
    // Code to create $content var here //
    $content = addSlashes($content);
?>

<script>
    <![CDATA[
        addBox('<?php echo $content; ?>');
    ]]>
</script>

如果您没有用 括住 Javascript,那么如果角度会出现错误找到括号,因为它会被解释为 HTML 标记的开头。

希望这有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文