dongzhuo1880 2013-11-16 14:19
浏览 122

表单输入数组$ _GET逗号分隔在URL中

I have a form:

<form action="results.php" method="get">
<select name="genres[]">
<option value="jazz"></option>
<option value="blues"></option>
<option value="rock"></option>
</select>
</form>

After submitting the form, in the URL the input becomes example.com/?genres%5B%5D=jazz&genres%5B%5D=blues&genres%5B%5D=rock

Next to that it doesn't look very nice, I have multiple inputs in my form (and around 18 genres) so if a user selects a lot, the URL becomes very long. I'd like it to become /?genres=jazz,blues,rock

Now I've read this post, but it doesn't state how to make the cleaner URL. Next to that, I have to use the input as an array, eg $genres = $_GET['genres'] to use in another function.

  • 写回答

1条回答 默认 最新

  • dptiq46022 2013-11-16 15:05
    关注

    you can do it like this:

    <!doctype html>
    <html>
    <head>
        <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    
        <script>
            $(document).ready(function(){
               $("#go").click(function(){
                   var g = $("#genres").val();
                   var href = 'results.php?genres=';
                   if(g != null) {
                       href += g;
                   }
                   document.location.href=href;
               });
            });
        </script>
    </head>
    <body>    
    <select id="genres" size="3" multiple>
    <option value="jazz">jazz</option>
    <option value="blues">blues</option>
    <option value="rock">rock</option>
    </select>
    <input type="button" value="go" name="submit" id="go">
    </body>
    </html>
    

    in results.php you should:

    $g = isset($_GET['genres'])?$_GET['genres']:"";
    $genres = explode(",",$g);
    

    i have removed the form completely because the genres are now sent by document.location.href. The .val() function of jquery returns the selected values like expected (imploded by ,)

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)