从表单select中获取参数值而不是查询字符串 [英] get parameter value from form select instead of querystring

查看:167
本文介绍了从表单select中获取参数值而不是查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个URL结构,其中包含查询字符串和参数,名称为 position 示例:

  http://computerhelpwanted.com/jobs/?occupation=administrator&position=network+administrator 

我还有一个窗体选择选项下拉列表,其中窗体选择名称为 position



例子:

 < form action =/ jobs / method =GETid =menuform> 

< select name =占用onChange =填充(this.id,\'position \')>
< option selected =disabled =>选择职业< / option>
< option value =administrator>管理员< / option>
< / select>

< select name =positiononChange =this.form.submit()>
< option selected =disabled =>选择位置< / option>
< option value =网络管理员>网络管理员< / option>
< / select>

< / form>

当用户进行选择时,它会将选项值以选择名称= 位置作为查询字符串中使用的参数。



我的问题是,如何从查询字符串值中单独访问表单选择值?



我使用_GET方法从查询字符串参数调用值。



示例:

  $ position = isset($ _ GET ['position'])? ($ _GET ['position']):''; 

显然,它从URL结构中获取值,而不是表单元素。或者,它可以做到这一点,不确定。但测试它,我似乎得出结论,它是从URL获取它,而不是形式。



如何确保在进行比较时访问表单选择值在我的PHP?



更新



我的标准网址设置在标题中。

 < link rel =canonicalhref =http://computerhelpwanted.com / jobs /?occupation = administrator& position = network-administrator/> 

该链接是Google上的索引链接。我知道我可以做一个htaccess重定向到新的链接,但我只是想弄清楚如何显示此页面的规范网址。



它应该是

 < link rel =canonicalhref =http://computerhelpwanted.com/jobs/?occupation=administrator& position = network + administrator/> 

唯一的区别是 - + 在查询字符串中。



并非所有的查询字符串都带有+。有些人拥有 - 。但是,我在两个URL上显示内容,无论它是 - 或+。无论哪种方式,这两个网址都会获得相同的页面内容。



但是,由于规范是从URI动态创建的,而不是来自表单元素的值,有2个不同的规范。



使用_Get('value')将查询字符串中的值拉到表单元素中。我知道这是因为表单元素值在网络管理员之间有一个空格,当表单提交为 network + administrator 时, 。因此,如果我可以比较表单元素的值,我可以设置正确的规范。

解决方案

HTTP是一种无状态协议,在调用所需的页面时生成HTML内容,然后在那里。当与表单元素进行交互时,可以从预定义(使用DOM构建)选项中选择值,并且可以将其传递给另一个文件进行处理。当你将这些值传递给处理器文件时,你可以通过各种方法来实现这一点(为了简单起见,我们只需看一下GET和POST文件就可以了。)

GET :会将您的表单参数转换为您在问题中发布的网址查询。 当您的数据以GET数组的形式到达您的处理器文件时,文件本身不知道它来自何种格式,它只能看到该查询。



POST:会将您的参数编码到传输层,使其看起来不明显,但仍然存在。使用GET,您可以手动形成一个含有无效或恶意参数的查询,并与他们一起调用处理器文件。 POST也可以通过编程方式设置为恶意的,但提供了一个方便的遮蔽等级,并考虑到您的问题,可能会提供您正在寻找的效果(不直接从url中取出)。



>编辑

根据OP问题和评论中可见模式简化表单参数处理的代码:

  $ validKeys =数组(
'位置',$ b $'职业'
) ;

foreach($ validKeys AS $ key){
$ {$ key} = isset($ _ GET [$ key])? $ _GET [$ key]:'';
}

/ *其他处理代码* /

如果你有15个输入参数,那么你就不需要一一浏览所有参数,因为 $ {$ key} 可以让你创建动态命名的变量,并且您只需更新 $ validKeys 容器(也可以用数据库中保存的值填充,而不是手动控制)。从你更新的评论中,我发现你可能正在寻找类似的东西。


I have a URL structure with query string and parameter called position.

example:

http://computerhelpwanted.com/jobs/?occupation=administrator&position=network+administrator

I also have a form select option drop down list with the form select name of position.

example:

<form action="/jobs/" method="GET" id="menuform">

<select name="occupation" onChange="populate(this.id,\'position\')">
<option selected="" disabled="">Select Occupation</option>
<option value="administrator">Administrator</option>
</select>

<select name="position" onChange="this.form.submit()">
<option selected="" disabled="">Select Position</option>
<option value="network administrator">Network Administrator</option>
</select>

</form>

When the user makes a selection, it sends the option values to the action attribute with the select name="position" as the parameter to use in the query string.

My question is, how do I access the form select values separately from the query string values?

I use the _GET method to call the value from the query string parameter.

example:

$position = isset($_GET['position']) ? ($_GET['position']) : '';

Apparently that gets the value from the URL structure, not the form element. Or maybe it does both, not sure. But testing it, I seem to draw conclusion that it is getting it from URL, not form.

How can I make sure to access the form select value when making my comparisons in my PHP?

Update

The issue I'm having is with my canonical URL set in the header.

<link rel="canonical" href="http://computerhelpwanted.com/jobs/?occupation=administrator&position=network-administrator" />

that link is a indexed link on Google. I know I can just do an htaccess redirect to new link, but I'm just trying to figure out how to display the canonical url for this page.

it should be

<link rel="canonical" href="http://computerhelpwanted.com/jobs/?occupation=administrator&position=network+administrator" />

the only difference is the - and the + in the query string.

Not all of my query strings have the +. Some have the -. But I display the content on both urls whether it has - or +. Either way, both urls get same page content.

But since the canonical is being created dynamically from the URI instead of what the value is from the form element, both content pages have 2 different canonicals.

Using _Get('value') is pulling the value from query string instead of form element. I know this because the form element value has a space between network administrator which gets urlencoded when form submits as network+administrator. So if I can compare to the form element value, I can set the proper canonical.

解决方案

HTTP is a stateless protocol, you generate the HTML stuff upon invoking the required page, and then it's there. When interacting with a form element, you can put select values from the predefined (built with DOM) options, and you can pass that on to another file for processing. When you pass the values to that processor file, you can do so through various methodologies (for simplicity lets just take a look at the GET and the POST ones).

GET: will convert your form parameters to a url query which you have also posted in your question. When your data arrives in the form of the GET array to your processor file, the file itself has no idea what kind of form it got it from, it only sees the query.

POST: will encode your parameters into the transport layer, having it not apparently visible, however it is still there.

Using GET, you could manually form a query with intendedly invalid or malicious parameters and invoke your processor file with them. POST can also be programatically set to be malicious, but provides a convenient level of obscuration as such, and considering your question, might provide the effect you were looking for ("not directly taking it from the url").

Edit:

Code for simplifying form parameter handling according to visible pattern seen in OP question and comment:

$validKeys = array(
    'position',
    'occupation'
);

foreach ($validKeys AS $key){
    ${$key} = isset($_GET[$key]) ? $_GET[$key] : '';
}

/* other processing code */

With the above, if you had lets say 15 input parameters, you wouldn't have to go through all of them one-by-one, as ${$key} allows you to create dynamically named variables, and you just update your $validKeys container (which could also be filled with values held in the database, and not manually controlled). From your updated comment I devised that you might be looking for something like this.

这篇关于从表单select中获取参数值而不是查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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