I'm using a form to enter a zipcode then running the JS and API to find zips in a radius. I was able to get that to work and return a string of them. Now I'm trying to use that information to filter users. I believe I need to use ajax but I can't get it to fire.
This is the jquery:
// JavaScript Document
function getZips(){
var nzip = $("#zip").val();
var radius = $("#radius").val();
var num_results = $("#num_results").val();
var zips="";
var resultsHTML = "";
var resultsTable= "";
$("#results").html("Loading...");
$.getJSON("http://www.zipcodeapi.com/api.php?method=getClosestzip&jsoncallback=?",
{
zip: nzip,
radius: radius,
nresults: num_results,
key: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
},
function(data) {
var x;
var i=1;
for (x in data)
{
var floatDistance = parseFloat(data[x].distance.toString());
resultsTable = resultsTable + data[x].zip.toString() + ', ';
i++;
}
$.ajax({
type: "GET",
url: "[[~22]]",
data: "resultsTable="+$(this).text(),
success: function(html) {
$('#users').replaceWith(html);
}
})
}
);
}
This is the snippet in [[~22]]
[[!pdoUsers? &tpl=`userListTpl` &where=`{"modUserProfile.fax:=":"0000000000","modUserProfile.comment:=":"Driver","modUserProfile.zip:=":"$_GET.resultTable"}` &limit=`0` &useExtended=`1`]]
Thanks for any help.
Everything I know I learned on the internet. Saved me thousands in College tuition,
-
☆ A M B ☆
- 24,524 Posts
You have to specify some kind of trigger event, like a click or the loading of the page.
Using an unvalidated GET value like that is very dangerous. Anybody can request that page with a malicious URL and hack your site.
Here's a pure PHP method that should get you close if your server will allow the file_get_contents() call (If not you can use cURL). Get an API Key here:
https://zipcodedistanceapi.redline13.com/Register
$radius = $_GET['radius'];
$zip = $_GET['zip'];
$apiKey = 'replace with API key';
$output = '<h2>' . $zip . "</h2>\n";
$url = 'https://zipcodedistanceapi.redline13.com/rest/'
. $apiKey . '/radius.json/' . $zip . '/' . $radius . '/mile';
$response = $modx->fromJSON(file_get_contents($url));
$codes = array();
foreach ($response['zip_codes'] as $zipCode) {
$codes[] = $zipCode['zip_code'];
}
$where = array(
'modUserProfile.fax' => "0000000000", // ?
'modUserProfile.comment' => 'Driver',
'modUserProfile.zip:IN' => $codes,
);
$fields = array(
'tpl' => 'userListTpl',
'useExtended' => '1',
'limit' => '0',
'where' => $modx->fromJSON($where),
);
$output .= $modx->runSnippet('pdousers', $fields);
return $output;
If you need more detail from the API response (like city names and distances) you'll have to move the runSnippet() and some of the other code into the loop, but it will slow things down significantly and you might want to bypass pdousers and query the DB directly.
Thanks Bob, Finally getting back around to this project. I hate to be a newbie but how do I call that snippet in my form? I've attempted a number of things and I'm running blank. Thanks for the help.
Everything I know I learned on the internet. Saved me thousands in College tuition,
Just put the code above in a new snippet called ZipCode, save it, and put this tag on the page where you want the results to appear (don't forget to change the API key line):
Bob, could you look at this and see what's missing. It's showing all the users and not filtering anything.
<?php
// return zip codes within a given radius of a given zip code
// TO DO - validate POST values
if(isset($_POST['findzip'])) {
$myzip = $_POST['myzip'];
$radius = $_POST['radius'];
$api_key = $modx->getOption('zipcode_api');
$url = "https://zipcodedistanceapi.redline13.com/rest/$api_key/radius.json/$myzip/$radius/mile";
// cURL stuff
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
$response = utf8_encode($response);
$zips = $modx->fromJson($response);
foreach ($zips['zip_codes'] as $zip) {
$codes[] = $zip['zip_code'];
}
$where = array(
'modUserProfile.fax' => "0000000000", // ?
'modUserProfile.comment' => 'Driver',
'modUserProfile.zip:IN' => $codes,
);
$fields = array(
'tpl' => 'userListTpl',
'useExtended' => '1',
'limit' => '0',
'where' => $modx->fromJSON($where),
);
$output .= $modx->runSnippet('pdousers', $fields);
return $output;
}
return;
Everything I know I learned on the internet. Saved me thousands in College tuition,
I think this is probably wrong:
foreach ($zips['zip_codes'] as $zip) {
$codes[] = $zip['zip_code'];
}
I think it should be this, though it depends on what $zips looks like.
foreach ($zips['zip_codes'] as $zip) {
$codes[] = $zip;
}