Passing a URL variable by submitting an HTML form using PHP or JavaScript

I just need to pass the form variable to the URL variable. I suspect this is something easy to do, but it's hard for me to find clear steps (these are not tons of code) on the Internet.

Here is 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 to do is send the browser to something like this:

 http://www.mydomain.com/dealers.php?zip=55118 

Thanks in advance for any help.


Refresh Question

Thanks Drew and Anton respond here to the update. Changing the login attribute to the name of the var (tZip to zip) URL along with changing the POST to GET did the trick, but for some reason it added two more additional URL variables (& x = 0 & y = 0). I assume that this is something wrong with my PHP code, since I am not a PHP master at any stage. Here is the whole code:

PHP function

 <?php function processForm() { $zipCode = $_GET['zip']; $url = "dealers.php?zip=" . $zipCode; header("Location: $url"); exit; } ?> 

The 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

 http://www.domain.com/dealers.php?zip=12345&x=0&y=0 

Additional related question

How does it work if processForm () is defined but not called anywhere. It seems to me that the function processForm () should be in the action attribute in the opening form element. Any insight? Thanks in advance.

+4
source share
2 answers

You will need to change the form method from POST to GET, as well as rename the text from tZip to zip , otherwise your URL will look like this:

http://www.mydomain.com/dealers.php tZip = 55118

instead

http://www.mydomain.com/dealers.php zip = 55118

+2
source

Change the form method to "get"

+4
source

All Articles