Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Redirecting user based on zip code entered

    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!

  • #2
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    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.

    Code:
    <html>
    <body>
     <form action="3.php" method="post">
    zip code: <input type="text" name="zip" />
    <input type="submit" />
    </form>
     </body>
    </html>
    PHP Code:
    <?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 -- is a huge help


    I hope I made some sense.. If you want me to clarify please feel free to ask.
    Last edited by stevenmw; 02-22-2012 at 12:04 PM.

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by stevenmw View Post
    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.

    Code:
    <html>
    <body>
     <form action="3.php" method="post">
    zip code: <input type="text" name="zip" />
    <input type="submit" />
    </form>
     </body>
    </html>
    PHP Code:
    <?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 -- 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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •