jQuery Problem receiving html variable from PHP script
I'm generating a bunch of tinymce edit boxes by sendi开发者_如何学运维ng 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;
}
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!
精彩评论