通过删除空的 GET 变量和简化变量名称来缩短 URL [英] Shorten URL by removing empty GET variables and simplifying variable names

查看:17
本文介绍了通过删除空的 GET 变量和简化变量名称来缩短 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个网站上工作,在该网站上提交了一个 GET 表单 后会组成一个 URL.表单值作为一组变量传递,必须至少定义一个变量才能使数据库搜索工作.我想通过删除空表单元素来缩短 URL,并通过简化变量名称使其更加用户友好.

I am working on a website where an URL is composed after submitting a GET form. The form values are passed as an array of variables of which at least one must be defined for the search on the database to work. I would like to shorten the URL by removing the empty form elements and to make it more user-friendly by simplifying the variable names.

目前,URL 看起来像这样(只是有更多的变量):

At the moment, the URL looks like this (just with a lot more variables):

http://localhost/example/search?FormName[name]=lorem+ipsum&FormName[id]=&FormName[age]=&yt0=Search

我的目标是让它看起来像这样:

I aim to make it look like this:

http://localhost/example/search?name=lorem+ipsum

为了做到这一点,我有以下问题:

In order to do this, I’ve got the following questions:

  • 我了解到,在使用 GET 方法时,仅使用 PHP 无法删除空表单元素,因为这是 html 表单的标准行为.有没有办法用 yii 的 urlManager 来做到这一点?

我可以将FormName[name]"替换为name"之类的更短的内容而不更改变量名称,例如使用正则表达式?

Can I replace "FormName[name]" with something shorter like "name" without changing the variable name, e.g., with regular expressions?

任何帮助将不胜感激.

推荐答案

简单的方法,如果 jQuery 是一个选项:

Simple method, if jQuery is an option:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
    (function($) {
        $('form').submit(function() { // ## Clean GET on Submit
            $('form input').each(function() { // ## Check each Input
                if ($(this).val().length == 0) { // ## If Empty
                    $(this).attr('disabled', true); // ## Disable Input
                }
            });
        });
    })(jQuery);
</script>

这篇关于通过删除空的 GET 变量和简化变量名称来缩短 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆