...

View Full Version : Redirecting user based on zip code entered



bela
02-22-2012, 08:09 AM
Hello All:

I am new to PHP and MYSQL and was wondering if i could get some help with having a user redirected to another web page based on the zip-code that they will enter. I have tried the javascript option but that came back with no results besides i read that it is not the best option.

Please help!

stevenmw
02-22-2012, 01:01 PM
A simple idea would be to have a list of possible zip code entries and match a users' submission to one of those entires.

Below is a very rough example of what I think you want to achieve.



<html>
<body>
<form action="3.php" method="post">
zip code: <input type="text" name="zip" />
<input type="submit" />
</form>
</body>
</html>


<?php
$zip1 = "74554";
$zip2 = "74501";
$zip3 = "74502";
$zipform = $_POST['zip'];

if ($zipform == $zip1) {
header("Location: http://google.com");
}

elseif ($zipform ==$zip2) {
header("Location: http://msn.com");
}

elseif ($zipform ==$zip3) {
header("Location: http://yahoo.com");
}
?>
Like I said above. This is just an example. (hopefully gives you a few ideas) I wouldn't EVER use this code on a functioning website.

One major thing I would do is incorporate a database. I would have a database table that contains all information regarding a zip code.

The reason I would use a DB is for easy maintaining. Each time you want to add a new zip code you could easily add it to the database without rewriting any source code.

For example. I would create a table on a database. This table would include three key pieces of information.

- id
- zip code
- page to redirect to when zip code is entered

This way you can constantly add zip codes to the DB. Then I would use a while loop to set up some predefined way to call out each zip code and all of its related information from the database.

Simply saying.. I would have a predefined code that basically fills in the required elements from the database.

Search around the internet for tutorial on some of the following functions.

- if statements
- if else statements
- $_POST or $_GET
- while loops
- mysql_query

Also look into basic database integration.

http://www.php.net (http://www.php.net/) -- is a huge help


I hope I made some sense.. If you want me to clarify please feel free to ask.

bela
02-25-2012, 08:27 PM
A simple idea would be to have a list of possible zip code entries and match a users' submission to one of those entires.

Below is a very rough example of what I think you want to achieve.



<html>
<body>
<form action="3.php" method="post">
zip code: <input type="text" name="zip" />
<input type="submit" />
</form>
</body>
</html>


<?php
$zip1 = "74554";
$zip2 = "74501";
$zip3 = "74502";
$zipform = $_POST['zip'];

if ($zipform == $zip1) {
header("Location: http://google.com");
}

elseif ($zipform ==$zip2) {
header("Location: http://msn.com");
}

elseif ($zipform ==$zip3) {
header("Location: http://yahoo.com");
}
?>
Like I said above. This is just an example. (hopefully gives you a few ideas) I wouldn't EVER use this code on a functioning website.

One major thing I would do is incorporate a database. I would have a database table that contains all information regarding a zip code.

The reason I would use a DB is for easy maintaining. Each time you want to add a new zip code you could easily add it to the database without rewriting any source code.

For example. I would create a table on a database. This table would include three key pieces of information.

- id
- zip code
- page to redirect to when zip code is entered

This way you can constantly add zip codes to the DB. Then I would use a while loop to set up some predefined way to call out each zip code and all of its related information from the database.

Simply saying.. I would have a predefined code that basically fills in the required elements from the database.

Search around the internet for tutorial on some of the following functions.

- if statements
- if else statements
- $_POST or $_GET
- while loops
- mysql_query

Also look into basic database integration.

http://www.php.net (http://www.php.net/) -- is a huge help


I hope I made some sense.. If you want me to clarify please feel free to ask.
Thank you so much. This was the exact thing that i needed.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum