Forum Moderators: coopster
I'm fairly new to the php/mysql world. I need some help with my first assignment doing some php scripting and was going to see if there was a resource of scripts for this.
Here's what I need to do: Setup a simple zip code search form that will execute a script. A user enters the zip code and it redirects them to one of four pages which is associated with different zip codes (in the state of Georgia). If they enter a zip code that's not on the list, it would redirect them to a page that says something like "this zip code is not a part of the area....etc.". I know how to set up the form to execute the script, just don't know what the script itself should be. I'm assuming I won't need to use a DB, and that the zips would be in the script itself. Any ideas to help? Thanks.
This seems pretty straightforward. Are you familiar with PHP's control structures [php.net] for handling logic?
looks like you need a switch [php.net]
but also think this way the switch could just assign the url to a variable and then you could use a single header call at the end
$url = '';
switch ($i) {
case 0:
$url = '/page1.html';
break;
case 1:
$url = '/page2.html';
break;
case 2:
$url = '/page3.html';
break;
}
header("Location:" . $url);
$url = '';
switch ($i) {
case 0:
case 4:
case 6:
$url = '/page1.html';
break;
case 1:
$url = '/page2.html';
break;
case 2:
case 5:
$url = '/page3.html';
break;
}
header("Location:" . $url);
they dont have to be numbers like that either, they could also be strings
case 'zip1':
etc
You need to do it as in Jatar_K's last example.
How many zip codes total are you looking at? Beyond a certain number, you really would want to use a database. That number is partly dependent on how comfortable you are with databases, of course.
Put all your zip codes in four arrays.
Use the in_array() function to test
$loc1 = array (53701, 53703, 53705);
$loc2 = array (53700, 53702, 53706);
$loc3 = array (53709);
// I don't know where you're info is coming from, but assume it's sent
// to the page as a GET param, which is a string no matter what (even if numeric)
// We grab just the first five chars and cast as an integer.
// We could test that it is numeric, but casting as an integer will strip
// out anything harmful and we'll test later to see whether it's one of our
// locations, which is a more restrictive test anyway.
$search_zip = (int) substr($_GET['location'], 0, 5);
// - next check to see which location array has this zip code
// if none, then location = 0 - not in our service area
if (in_array($search_zip, $loc1))
{
$location = 1;
}
elseif (in_array($search_zip, $loc2))
{
$location = 2;
}
elseif (in_array($search_zip, $loc3))
{
$location = 3;
}
else
{
$location = 0;
}
// Now redirect to the correct page or to the "Sorry no service page"
if ($location)
{
header("Location: http://example.com/locations/location" . $location . ".php");
}
else
{
header("Location: http://example.com/locations/noservice.php");
}
Here is the form code to call up the script:
<form name="" method="post" action="redirect.php">
<input name="zip" type="text" id="zip" size="10">
<input type="submit" name="Submit" value="Go">
<br>
</form>
redirect.php is the script file of course. Contains th e following code:
<?php
$url = '';
switch ($zip) {
case "30002":
case "30012":
case "30013":
case "30014":
case "30015":
case "30016":
case "30017":
case "30018":
case "30021":
case "30025":
case "30030":
case "30031":
case "30032":
case "30033":
case "30034":
case "30035":
case "30036":
case "30037":
case "30038":
case "30039":
case "30047":
case "30048":
case "30052":
case "30054":
case "30055":
$url = '/scottdale.php';
break;
default:
$url = '/out_network.php';
break;
}
header("Location:" . $url);
?>
Honestly, i'm just trying to get this to work and for some reason cannot. It just keeps going to out of network link and i'm not sure what I've done wrong. I've tried to Debug it, but can't figure it out. :(