使用GET方法发送空数据 [英] sending null data using GET method

查看:151
本文介绍了使用GET方法发送空数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用php / sql创建一个非常原始的搜索功能。
基本上,我使用GET方法在 /search.php中的几个字段提交表单查询。



所以查询将以下面的形式发送:

  clothes.php?type = sometype& color = somecolor& amp ; mat = somemat& pat = somepattern& uprc = price& brand = brand 

然而,我有脚本clothes.php,这样它的所有字段都不必填写成功查询。



clothes.php?type = sometype 将返回指定类型的衣服。



对于 search.php 中的所有字段,I我使用(选择)和(选项)列出每个字段下的可用选项。



问题是,在 /search.php ,我想阻止search.php发送表单数据,如果一个特定的字段为空。



我试着创建<选择name =type>< option value =>< / option> ...< / select> 作为每个下拉框中的第一个选项,但它仍然在该字段下发送一些数据。



是否有解决方法?

),例如富=&安培;巴=巴兹 null 是不可能的,HTTP没有 null 值的概念。如果你想从这个查询字符串中删除空的 foo ,你必须在提交表单时在客户端上使用Javascript。因为总是有没有Javascript的客户端,所以总是会有你收到一个空的 foo 的情况,所以你需要为它做好准备。



如果您坚持使用干净URL,则可以在服务器上重定向并过滤掉空白参数。例如。通过POST提交搜索表单,然后用非空参数构建一个GET URL并重定向到它。尽管如此,这会产生额外的HTTP请求。


I am trying to create a very primal search function using php/sql. Basically, I am using the GET method to submit the form query over several fields in /search.php.

So the query will be transmitted in the following form:

clothes.php?type=sometype&color=somecolor&mat=somemat&pat=somepattern&uprc=price&brand=brand

However, I have scripted clothes.php such that it all fields do not have to be filled for a successful query.

clothes.php?type=sometype will return clothes of the specified type.

For all my fields in search.php, I am using (select) and (option) to list the available options under each field.

The problem is, in /search.php , I want to prevent search.php from sending form data if a particular field is empty.

I tried creating <select name="type"><option value = ""></option>...</select> as the first option in each dropdown box but it still sends some data under that field.

Is there a workaround for this?

解决方案

The best you can do using pure HTML is to submit empty strings ("") for empty values, e.g. foo=&bar=baz. null is not possible either way, HTTP has no notion of null values. If you want to remove the empty foo from this query string, you'd have to do so using Javascript on the client when the form is submitted. Since there are always clients without Javascript though, there'll always be cases were you receive an empty foo, so you need to be prepared for it anyway.

If you insist on "clean" URLs no matter what, you could redirect on the server and filter out empty parameters. E.g. submit the search form via POST, then build a GET URL with the non-empty parameters and redirect to it. This'll have the overhead of an additional HTTP request though.

这篇关于使用GET方法发送空数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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