【发布时间】:2015-11-16 20:53:58
【问题描述】:
我通过 AJAX 接收一些代码并像这样处理它:
$verifiedSubject = addslashes(htmlentities($_REQUEST['subject']));
$verifiedBody = addslashes(htmlentities($_REQUEST['body']));
$verifiedAttachment1 = addslashes(htmlentities($_REQUEST['attachment1']));
$verifiedAttachment2 = addslashes(htmlentities($_REQUEST['attachment2']));
echo '<div id="subject" style="text-decoration: underline; cursor:pointer; display: inline; margin-bottom: 2%;" onclick=\'readmore("'.json_encode($verifiedSubject).'", "'.json_encode($verifiedBody).'", "'.json_encode($verifiedAttachment1).'", "'.json_encode($verifiedAttachment2).'")\'>';
echo $_REQUEST['subject'];
echo '</div>';
在上面的代码中,我尝试将任何 HTML 代码转换为实体,添加斜杠以转义单引号和双引号,然后 json_encode() 它供 JavaScript 在 onclick 函数中处理。
然而,当点击文本启动onclick我得到这个错误:
Uncaught SyntaxError: missing ) after argument list
我尝试了各种 PHP 函数来尝试正确转义该字符串,但似乎没有任何效果。谁能帮帮我?
页面来源更新:
<script>
var date = "Monday, November 16th, 2015 Announcements";
formatAnnouncement( '55',
'Hi',
'<b rgb(0, 0, 0); font-family: Arial, Helvetica, sans; font-size: 11px; line-height: 14px; text-align: justify;\">Lorem Ipsum</b><span rgb(0, 0, 0); font-family: Arial, Helvetica, sans; font-size: 11px; line-height: 14px; text-align: justify;\"> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span>',
'0',
'',
'',
'******',
'2015-11-16 16:53:23',
date
);
</script>
【问题讨论】:
标签: javascript php jquery ajax escaping