4

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.

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:

PHP Function

<?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

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.

1
  • Others have suggested the solution for you. To get the essence of this I'd suggest you o follow this course cs75.tv/2009/fall Commented Nov 30, 2010 at 22:24

2 Answers 2

4

Change the form method to "get"

1
  • This along with the input name updated from tZip to zip did the trick. Thank you! I'm posting an update to my question as it added two subsequent URL vars. Am I over commenting on all the replies here? I'm still figuring out how to use Stack Overflow properly. Commented Dec 1, 2010 at 17:12
2

You will need to change the form method from POST to GET and also rename the text input from tZip to zip otherwise your URL will look like this:

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

instead of

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

1
  • Thank you, all! Drew definitely answered the question and got it working. Although for some reason it's adding two additional URL variables (zip=12345&x=0&y=0). I'll edit/add to my original question with this info. I think that's what I'm supposed to do. I'm new to Stack Overflow. Commented Dec 1, 2010 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.