表单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.   

    $变量 = urlencode($_GET['type']);
      

  2.   

    当点击提交时,url就已经是1.php?type=中文 了,没有机会 $变量 = urlencode($_GET['type'])的
      

  3.   

    把提交按钮的类型改变button,onclick时先编码文字,再更改文档的location.用js控制浏览器地址的变更。
      

  4.   

    用这么麻烦的么?一些大网站,例如百度,我们在搜索的时候,只要一点击“百度一下”,url就马上变urlencode后的格式了。这是怎么处理的呢?
      

  5.   

    我只想说你自己去百度首页看看那个按钮是什么,也是一个Input, 也是在一个form里,form默认就是urlencode提交参数的,get参数apache会decode的,post参数php会decode的,你拿到的永远都是原封不动的数据,除非客户端直接post一堆东西上来,Content-type不是form-data,那么php就原封不动的交给你了,叫做raw_post_data。百度是大网站,大网站要考虑那些没有开启JS的同学的状况,所以不可能是一个普通按钮+JS实现表单的。
      

  6.   

    我只是想知道提交之前怎样处理url 将其变成urlencode后的码
      

  7.   


    好像post参数也是apache解析的,应该是的,只要与http协议相关的,都应该由web服务器解析。
    只有content-type无法识别的时候才完整的交给php。
      

  8.   

    跟浏览器版本有关系么?我怎么得到的url都传着中文呢?
      

  9.   

    你如果非得这样,那干脆在FORM提交时进行处理
    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'>
      

  10.   

    你把中文urlencode两次就可以在服务端获得js的urlencode源码了。需要ajax设置setRequestHeader为application/www-form-data,是不是这么拼来着。
      

  11.   


    刚刚仔细测试了一下,首先阁下的代码有点问题,
    (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'> &nbsp;<input type='submit' value="Submit" />&nbsp;<input type="button" value="Btn" onclick="To();" />
    </form>
    </body>
    </html>
      

  12.   

    继续补全test_36.php的内容,
    <?php
    header("Content-type:text/html;charset=utf-8");
    $s = $_REQUEST['key'];
    echo urldecode($s);注意所有文件都保存为utf8格式,