我需要简单地将表单变量传递给URL变量。我怀疑这很容易,但我很难在任何地方找到明确的步骤(不是很多代码)。
这是我当前的表单代码
<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>
我只需要将浏览器发送到这样的内容:
http://www.mydomain.com/dealers.php?zip=55118
提前感谢您的帮助。
感谢Drew和Anton的回复,这是一个更新。更改输入名称属性以匹配URL var名称(tZip到zip)以及将POST更改为GET,但是由于某种原因,它还添加了两个额外的URL变量(&amp; x = 0&amp; y = 0)。我猜这是我的PHP代码不正确,因为我不是任何篇幅的PHP向导。这是所有代码:
PHP功能
<?php
function processForm() {
$zipCode = $_GET['zip'];
$url = "dealers.php?zip=" . $zipCode;
header("Location: $url");
exit;
}
?>
表格
<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>
网址输出示例
http://www.domain.com/dealers.php?zip=12345&x=0&y=0
如果仅定义了processForm()但在其他任何地方都没有调用,那么它是如何工作的。在我看来,processForm()函数应该在开始表单元素的action属性中。任何见解?提前谢谢。
答案 0 :(得分:4)
将表单方法更改为“get”
答案 1 :(得分:2)
您需要将表单方法从POST更改为GET,并将文本输入从 tZip 重命名为 zip ,否则您的网址将如下所示:
http://www.mydomain.com/dealers.php?的 tZip 强> = 55118
而不是
http://www.mydomain.com/dealers.php?的拉链强> = 55118