Geocoder Error Checking

By gkornbluth - January 28, 2016 - edited: January 28, 2016

Hi all,

I’m just beginning to play with the Geocoder plugin and want to create some more robust error checking.

To determine if there are at least 5 characters in the Postal Code (and no more than 12) I’ve tried adding the code in red to my sample search page, but it doesn’t work quite right. (2 submission clicks are required to get to the results page, and the error check doesn’t work after a correct submission (I can go back, enter only 2 digits for the Postal code, click submit, and the 2 digits are no longer caught by the error checking.)

Any ideas about what I've missed that needs to be adjusted (or maybe I'm just re-inventing the wheel, and this kind of thing is already built in?)?

Thanks,

Jerry Kornbluth

  // error checking
  if (!@$GLOBALS['GEOCODER_PLUGIN']) { die("You must activate the Geocoder plugin before you can access this page."); }

  // set default values (not required)
  if (!array_key_exists('fromAddress', $_REQUEST)) { $_REQUEST['fromAddress'] = 'please enter a zip code'; }      // this is the default address/postalcode if none entered

  // get geocoding data
  list($myLat, $myLng) = geocodeAddress( @$_REQUEST['fromAddress'] );
  $kmOrMiles           = 'miles'; // can be 'miles' or 'km'
  $geoOptions          = geocoder_getOptions($myLat, $myLng, @$_REQUEST['maxDistance'], $kmOrMiles);

  // get records
  list($addressRecords, $addressMetaData) = getRecords(array(
    'tableName'     => $GLOBALS['GEOCODER_SAMPLE_TABLENAME'],
  ) + $geoOptions);  // geoOptions WILL NOT override the above options

   // error checking
  $errorsAndAlerts = '';
  $action = '' ;

  if (@$_REQUEST['save']) {
   @$count = '';
    @$count = mb_strlen($_REQUEST['fromAddress']  );
     if     (!@$_REQUEST['fromAddress']) { $errorsAndAlerts .= "No zip code entered.<br/>\n";
     }
    elseif (@$count < 5 )         { $errorsAndAlerts .= "Your zip code must be at least 5 digits long.<br/>It is currently under 5 digits.<br/>\n";
    }

  elseif ((!@$myLat || !@$myLng))         { $errorsAndAlerts .= "Please enter a valid zip code.<br/>\n"; @$_REQUEST['save'] = '';}
   if (!@$errorsAndAlerts && @$count > 4 && @$count <= 12)     { $action .= "http://dbtproviders.com/sample_search_resultsA.php"; }
  }

?>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By ross - January 29, 2016

Hi Jerry

Could you give me more detail about what you mean with 2 submission clicks are required to get to the results page?  

That might end up being the problem if the ZIP code is entered on the first submission but not checked until the second submission.

Let me know.

Thanks!

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

By gkornbluth - January 29, 2016

Hi Ross,

Thanks for looking at this.

Sounds like that might be the issue but I have no idea how to get around the problem.

You can try it yourself (one demo worth a thousand words, etc.)

http://dbtproviders.com/directory_searchB.php

Basically, the first click on the submit button with a valid zip code just  re-loads the search page, a second click brings up the results page.

Both the search form and the results page are attached.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By ross - February 1, 2016

Hi Jerry

I've looked through your code and I think there's a problem with the { } with your if blocks.

Take a look at this block of code:

if (@$_REQUEST['save']) {

     @$count = '';
     @$count = mb_strlen($_REQUEST['fromAddress']  );

     if     (!@$_REQUEST['fromAddress'])                    { $errorsAndAlerts .= "No zip/postal code entered.<br/>\n"; }
     elseif (@$count < 5 )                                  { $errorsAndAlerts .= "Your zip/postal code must be at least 5 digits long.<br/>It is currently under 5 digits.<br/>\n"; }

elseif ((!@$myLat || !@$myLng))                             { $errorsAndAlerts .= "Please enter a valid zip/postal code.<br/>\n"; @$_REQUEST['save'] = ''; }

if (!@$errorsAndAlerts && @$count > 4 && @$count <= 12)     { $action .= "http://dbtproviders.com/directory_resultsB.php"; }

}

I formatted the code a bit so it's easier to see what I'm talking about.

What I think you need to do is take that last } and move it up a bit like this:

if (@$_REQUEST['save']) {

     @$count = '';
     @$count = mb_strlen($_REQUEST['fromAddress']  );

     if     (!@$_REQUEST['fromAddress'])                    { $errorsAndAlerts .= "No zip/postal code entered.<br/>\n"; }
     elseif (@$count < 5 )                                  { $errorsAndAlerts .= "Your zip/postal code must be at least 5 digits long.<br/>It is currently under 5 digits.<br/>\n"; }
}

elseif ((!@$myLat || !@$myLng))                             { $errorsAndAlerts .= "Please enter a valid zip/postal code.<br/>\n"; @$_REQUEST['save'] = ''; }

if (!@$errorsAndAlerts && @$count > 4 && @$count <= 12)     { $action .= "http://dbtproviders.com/directory_resultsB.php"; }


Give that a shot and let me know how it effects your search.

Thanks!

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

By ross - February 2, 2016

Hi Jerry

I need to clarify what issue we are looking at here.

Is the problem that the error checking doesn't work or that it takes two submits to get to your results?

Let me know.

Thanks!

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

By gkornbluth - February 2, 2016

Hi Ross,

It's that it takes 2 submits to get the result

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By ross - February 4, 2016

Hi Jerry

Thanks for clarifying.  I recommend removing the error checking and getting your code as close to the generator code as possible. That should get the page submitting properly.  Then add the error checking back in bit by bit.

What this should do is get your form submitting with just one click. And as you add the error checking back in bit by bit, you'll eventually get to the part that's causing the double submit issue.

Does that make sense? 

Let me know how you make out.

Thanks!

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

By gkornbluth - February 4, 2016

Thanks Ross,

Good suggestion

I'll give it a try and let you know

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php