使用 PHP 或 JavaScript 通过 HTML 表单提交传递 URL 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4317922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing URL variable via HTML form submission using either PHP or JavaScript
提问by Brian Larson
I need to simply pass a form variable into a URL variable. I suspect that it's something easy to do but I'm having a hard time finding clear steps (that aren't tons of code) online anywhere.
我需要简单地将表单变量传递给 URL 变量。我怀疑这很容易做到,但我很难在任何地方在线找到明确的步骤(不是大量代码)。
Here's my current form code
这是我当前的表单代码
<form id="zip_search" method="post" action="dealers.php">
<label for="zipfield"><a href="dealers.php">Find a Dealer</a></label>
<input name="tZip" type="text" id="zipfield" value="ZIP CODE" onblur="if(this.value=='') this.value='ZIP CODE';" onfocus="if(this.value=='ZIP CODE') this.value='';" />
<input type="image" value="Submit" class="submitbutton" src="/images/submit_button.gif" />
</form>
And all I need is for it to send the browser to something like this:
而我所需要的只是将浏览器发送到这样的内容:
http://www.mydomain.com/dealers.php?zip=55118
Thank you in advance for any help.
预先感谢您的任何帮助。
Update to question
更新问题
Thanks to Drew and Anton's responses here's an update. Changing the input name attribute to match the URL var name (tZip to zip) along with changing POST to GET did the trick but for some reason it's adding two additional URL variables as well (&x=0&y=0). I'm guessing this is something incorrect with my PHP code as I'm not a PHP wizard by any stretch. Here's all the code:
感谢 Drew 和 Anton 的回应,这里有一个更新。更改输入名称属性以匹配 URL 变量名称(tZip 到 zip)以及将 POST 更改为 GET 可以解决问题,但出于某种原因,它还添加了两个额外的 URL 变量(&x=0&y=0)。我猜这是我的 PHP 代码不正确的地方,因为我无论如何都不是 PHP 向导。这是所有的代码:
PHP Function
PHP函数
<?php
function processForm() {
$zipCode = $_GET['zip'];
$url = "dealers.php?zip=" . $zipCode;
header("Location: $url");
exit;
}
?>
Form
形式
<form id="zip_search" method="get" action="dealers.php">
<label for="zipfield"><a href="dealers.php">Find a Dealer</a></label>
<input name="zip" type="text" id="zipfield" value="ZIP CODE" onblur="if(this.value=='') this.value='ZIP CODE';" onfocus="if(this.value=='ZIP CODE') this.value='';" />
<input type="image" value="Submit" class="submitbutton" src="/images/submit_button.gif" />
</form>
URL Output Example
URL 输出示例
http://www.domain.com/dealers.php?zip=12345&x=0&y=0
Additional Related Question
其他相关问题
How is this working if processForm() is only defined but not called anywhere else. It seems to me that the processForm() function should be in the action attribute in the opening form element. Any insight? Thanks in advance.
如果 processForm() 仅定义但未在其他任何地方调用,这如何工作。在我看来 processForm() 函数应该在打开表单元素的 action 属性中。任何见解?提前致谢。
采纳答案by Drew
You will need to change the form method from POST to GET and also rename the text input from tZipto zipotherwise your URL will look like this:
您需要将表单方法从 POST 更改为 GET,并将输入的文本从tZip重命名为zip,否则您的 URL 将如下所示:
http://www.mydomain.com/dealers.php?tZip=55118
http://www.mydomain.com/dealers.php?tZip=55118
instead of
代替
http://www.mydomain.com/dealers.php?zip=55118
http://www.mydomain.com/dealers.php?邮编=55118
回答by Brian H
Change the form method to "get"
将表单方法更改为“获取”