Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Quote即使在转义之后也会破坏语法_Javascript_Php_Yii2 - Fatal编程技术网

Javascript Quote即使在转义之后也会破坏语法

Javascript Quote即使在转义之后也会破坏语法,javascript,php,yii2,Javascript,Php,Yii2,我有一个按钮,它应该用两个参数(一个整数和一个字符串)调用一个操作。我用的是Yii2 <button class="button_answer" onclick="submitAnswer( <?php echo $id ?>, <?php echo '\''.Html::encode($title).'\''?> );"> Submit your answer </button

我有一个按钮,它应该用两个参数(一个整数和一个字符串)调用一个操作。我用的是Yii2

<button class="button_answer" 
    onclick="submitAnswer(
            <?php echo $id ?>, 
            <?php echo '\''.Html::encode($title).'\''?>
    );">
    Submit your answer
</button>  

您需要为JavaScript编码PHP字符串。然后需要将JavaScript编码为HTML

<?php
$js_string_title = json_encode($title);
$js = "submitAnswer($id, $js_string_title)";
$html_safe_js = htmlspecialchars($js);
?>

<button class="button_answer" 
     onclick="<?php echo $html_safe_js; ?>">
  Submit your answer
</button>  

您需要为JavaScript编码PHP字符串。然后需要将JavaScript编码为HTML

<?php
$js_string_title = json_encode($title);
$js = "submitAnswer($id, $js_string_title)";
$html_safe_js = htmlspecialchars($js);
?>

<button class="button_answer" 
     onclick="<?php echo $html_safe_js; ?>">
  Submit your answer
</button>  

好啊它正在工作。谢谢你的解释。我认为Yii框架的函数编码集成了htmlspecialchars函数+1.thanks@stfsngue-可能是的……但它没有理由合并
json\u encode
。现在我明白了。至少我刚刚从你那里学到了一些东西。谢谢我标记为已解决。好的。它正在工作。谢谢你的解释。我认为Yii框架的函数编码集成了htmlspecialchars函数+1.thanks@stfsngue-可能是的……但它没有理由合并
json\u encode
。现在我明白了。至少我刚刚从你那里学到了一些东西。谢谢我表示已决定。
<button class="button_answer" 
        data-id="<?php echo htmlspecialchars($id); ?>"
        data-title="<?php echo htmlspecialchars($title); ?>">
     Post your answer
</button>
addEventListener("click", answer_handler);

function answer_handler(event) {
     var el = event.target;
     if (el.classList.contains("button_answer")) {
         submitAnswer(el.dataset.id, el.dataset.title);
     }
}