【发布时间】:2017-09-27 19:39:04
【问题描述】:
我有以下 search.php 文件:
<?php
$q = $_GET["q"];
?>
<html>
...
<input id="inp" type="text">
...
...
<script>
// here i'm trying to avoid harmful user input with htmlspecialchars
document.getElementById("inp").value = "<?php echo htmlspecialchars($q); ?>";
</script>
...
</html>
当我请求 search.php?q=Adam(或任何 A-Z、a-z)时,它工作正常。但是当我搜索 " 或 \ 输入值不同于 $_GET["q"] 值时。
但如果我搜索 " 它会添加 \,请参见下图。
我需要的只是将输入值设置为 $_GET["q"]。有没有更好的解决方案?
【问题讨论】:
-
$q = htmlspecialchars($_GET["q"]) -
@SagarV — 第二次使用
htmlspecialchars有什么帮助?
标签: javascript php