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

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

问题描述

我在提交 GET表单后组成网址的网站上工作。表单值作为一个变量数组传递,其中必须至少定义一个用于数据库搜索的工作。我想通过删除空的表单元素来缩短网址,并通过简化变量名称使其更加便于用户使用。



目前,URL看起来像这样(只是有很多变量): b
$ b

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



我想让它看起来像这样:



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



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




  • 我读过使用GET方法时不可能仅用PHP删除空的表单元素,因为这是html表单的标准行为。有没有一种方法可以通过yii urlManager 执行此操作?

  • 我可以用一些更短的东西来替换FormName [name]吗?像名字而不改变变量名称,例如使用正则表达式?

  • 最后但并非最不重要的是:yt0 = Search signify以及如何从URL中删除它?



任何帮助都将不胜感激。

解决方案

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

 < ; script src =https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js>< / script> 
< script type =text / javascript>
(function($){
$('form')。submit(function(){// ##在提交
$('form input')时清理GET。 (){// ##检查每个输入
if($(this).val()。length == 0){// ## If Empty
$(this).attr('disabled ',true); // ##禁用输入
}
});
});
})(jQuery);
< / script>


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.

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:

  • I’ve read that it’s not possible to remove the empty form elements with only PHP when using the GET method because this is the standard behaviour of the html form. Is there a way to do this with the urlManager from yii?

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

  • And at last but not least: What does the "yt0=Search" signify and how can I remove it from the URL?

Any help would be greatly appreciated.

解决方案

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天全站免登陆