表单get方式提交,怎样给参数urlencode编码
例如:
1.php
<form method="get" action="">
请输入类型:<input type="text" name="type" />
<input type="submit" value="提交" />
</form>
在文本框中输入中文,点击提交,这时得到的url是1.php?type=中文,
我怎样才能得到1.php?type=urlencode(中文)的url呢?
例如:
1.php
<form method="get" action="">
请输入类型:<input type="text" name="type" />
<input type="submit" value="提交" />
</form>
在文本框中输入中文,点击提交,这时得到的url是1.php?type=中文,
我怎样才能得到1.php?type=urlencode(中文)的url呢?
好像post参数也是apache解析的,应该是的,只要与http协议相关的,都应该由web服务器解析。
只有content-type无法识别的时候才完整的交给php。
1.php<script type='text/javascript'>
function sub()
{
window.location.href='1.php?input1='+encodeURI(document.getElementByName('input1'));
return false;
}
</script>
<?php
echo $input1;
?>
<form name='form1' method='get' onSubmit='return sub();'>
<input type='text' name='input1'>
<input type='submit'>
刚刚仔细测试了一下,首先阁下的代码有点问题,
(1) document.getELementByName 这个方法是存在的,但是这样得到的会使一个数组类型的值,
(2) 改成用document.getELementById之后,用form表单的onsubmit事件进行类似的处理, 发现在firefox下仍然无法达到想要的效果, 但是在IE下正常,应该是firefox会对地址栏内的参数自动进行decode,
最后如果想要得到类似baidu的效果,可以参考下面的代码:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type='text/javascript'>
function redirect() {
href = "test_36.php?key="+encodeURI(document.getElementById('input1').value);
location.href = encodeURI(href);
//alert(href);
return false;
}function To() {
href = "test_36.php?key="+encodeURI(document.getElementById('input1').value);
location.href = encodeURI(href);
}</script>
</head>
<body>
这个实例实现的是: 保证从表单传递变量到另外一个页面时, 中文部分被编码,
<form name='form1' method='get' onsubmit="return redirect();">
<input type='text' id='input1' name='input1'> <input type='submit' value="Submit" /> <input type="button" value="Btn" onclick="To();" />
</form>
</body>
</html>
<?php
header("Content-type:text/html;charset=utf-8");
$s = $_REQUEST['key'];
echo urldecode($s);注意所有文件都保存为utf8格式,