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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to have form to give results to user based on zip code

    I want to have a small form on my homepage that has a space for a user's email address and city, state, zip code. Based on the input, I want the form to generate an automatic response to the user. For example, if my site deals with dentists, I want the form to automatically send her the names and contact info of 4 dentists in that area (provided they are dentists listed on my site). How would I go about doing this? Any info would be greatly appreciated. Thanks!

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You want to do an exact match on zip code? Or you want to find zip codes geographically close to the zip code entere on the form? The two requirements will be vastly different in complexity.

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    I've done this before, you will need to do a few things here;

    first you will need to get the lat/long or the addresses in question, you can do this using google maps api, this is the function that I build:

    PHP Code:
        Function GetLatLng($Address,$Type){
            
    // request URL
            
    $url "http://maps.google.com/maps/geo?q=" urlencode($Address) . "&output=xml&key=" GOOGLE_MAP_KEY;

            
    // get file
            
    $ch curl_init($url);
            
    $Temp LOCAL_TEMP_PATH "GoogleLocation.xml";
            
    $fp fopen($Temp"w");
            
    curl_setopt($chCURLOPT_FILE$fp);
            
    curl_setopt($chCURLOPT_HEADER0);
            
    curl_setopt($chCURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // makes our request look like it was made by Firefox
            
    curl_exec($ch);
            
    curl_close($ch);
            
    fclose($fp);

            
    // read file
            
    $page = @file_get_contents($Temp);
            
    $xml = new SimpleXMLElement($page);
            
    $Result $xml->Response->Placemark->Point->coordinates;
            
    $LatLng explode(",",$Result);
            if(
    $Type == "Lng") return $LatLng[0];
            if(
    $Type == "Lat") return $LatLng[1];
        } 
    send request:
    PHP Code:
        function SendRequest($url){
            
    //echo $url; exit;
            
    $ch curl_init($url);
            
    curl_setopt($chCURLOPT_RETURNTRANSFER,1); // forces response into return string, not echo
            
    curl_setopt($chCURLOPT_HEADER0);
            
    curl_setopt($chCURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") );
            return 
    curl_exec($ch);
            
    curl_close($ch);
        } 


    you should store this data in your database, same table as the dentists,

    then you need to get the coordinates of the client ( person doing the lookup )

    PHP Code:
        function getCoord(){
            
    $d $this->SendRequest("http://maps.google.com/maps/geo?q=" urlencode($this->PostalCode) . "&output=xml&key=" GOOGLE_MAP_KEY);
            if (!
    $d)
                return 
    false// Failed to open connection

            
    $coord = new SimpleXMLElement($d);

            if ((string) 
    $coord->Response->Status->code != '200')
                return 
    false// Invalid status code

            
    list($lng$lat) = explode(',', (string) $coord->Response->Placemark->Point->coordinates);
            return array(
    'Lat' => (float) $lat'Lng' => (float) $lng);
        } 
    then, you need to calculate the distance, and display the list;

    PHP Code:
        $dst1 $StoreLocator->getCoord(); // for user lng/lat
        
    $Distance "";
        
    // get store locations lng/lat
        
    $result mysql_query("SELECT City,LocationsID,ProvinceName,City,MapLat,MapLng FROM Locations l, Provinces p WHERE l.LocationsID !=34 AND l.ProvincesID = p.ProvincesID ORDER BY LocationsID ASC");
        While(
    $row mysql_fetch_array($result)){
            
    $kms rad2deg(acos(sin(deg2rad($dst1['Lat'])) * sin(deg2rad($row['MapLat'])) + 
            
    cos(deg2rad($dst1['Lat'])) * cos(deg2rad($row['MapLat'])) *  
            
    cos(deg2rad($dst1['Lng'] - $row['MapLng'])))) * 60 1.1515 1.609344;
            
    $Distance[] = array(round($kms,1),$row['LocationsID'],$row['City'],$row['ProvinceName']);
        }

        
    $Counter 0;
        
    $DistanceArray $StoreLocator->msort($Distance,0);

        foreach (
    $DistanceArray as $value){
            
    $Counter++;
            echo 
    "<div class='LocationCityRow' onClick=\"location.href='/location/" $value[1] . "/';\">" $value[3] . " &bull; " $value[2] . " &bull; Distance: " $value[0] . "Kms</div>\n";
            if(
    $Counter == 5) break;
        } 

    ofcourse these functions are built for my site, but you can mod them to work for you.
    hope this helps.

  • #4
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the help, but I think this is probably way over my head.


  •  

    Posting Permissions

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