代码如下,urlencode或rawurlencode函数为什么没有对输入的内容生效?
比如输入中文或其他特殊字符,应该输出编码字符串才对吧??但是没有生效
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
<input type="text" id="txt" name="txt" value="">
<button type="button" onclick="test();">点击</button>
<?
$test = "<script type=text/javascript>function test(){var txt= document.getElementById('txt').value;var data = ".rawurlencode('txt').";document.write(data);}</script>";
echo $test;
?>
</body>
</html>
修改后代码
<?
$test = "<script type=text/javascript>function test(){var txt= document.getElementById('txt').value;var data = '".urlencode('测试')."';document.write(data);}</script>";
echo $test;
?>
修改后效果
你现在这个代码生成的网页是这样的:
谢谢@黑白码农,虽然不是我要的效果,而且js实现我会,但是php函数实现我已经知道为什么无法实现了,因为php echo作为输出实际上已经渲染到前端了,所以无论怎么前端test()调用函数都是无法再次调用php函数,所以悬赏还是给你吧,感谢你多次回答让我醍醐灌顶。
这条回复作为希望以后遇到同样问题的人