Add Geolocation Information to Order Forms

Hi John,

Many thanks for the pointer, with your advice it was a simple solution.

You just had to call ajax on page onload / jQuery(document).ready(function() for everything to work and as you said, not calling ajax via a script avoids the infinite page refresh/reload issue.

Now I’m just working on getting the TimeZone field value to populate. For some reason it is not doing so or alternatively the freegeoip.net is not pulling that dataset value during testing. Will update with that final solution when figured out.

Suggested mod by @Pav to drop the lines of code for TimeZone

jQuery('#Contact0TimeZone').val(location.time_zone);
<input name="Contact0TimeZone" id="Contact0TimeZone" type="hidden" value="" />

and use the built-in function already called by Infusionsoft.

The Hidden Element is called “timeZone”, ( jQuery(’#timeZone’).val() ). There is a “/resources/timezone/timezone.js” script that is being loaded by Infusionsoft. 

This mod will likely avoid any possible duplicate field value update conflicts between the built-in code & manually applied code. Have left the code example as it currently is, in case anyone wants the TimeZone value populated by the Free Geo Service.

Here is the working code.

<script>
	jQuery(document).ready(function(){
	jQuery.ajax( {
		url: '//freegeoip.net/json/',
		type: 'POST',
		dataType: 'jsonp',
		success: function(location) {
			// Update hidden form field variable values
			jQuery('#Contact0City3').val(location.city);
			jQuery('#Contact0State3').val(location.region_code);
			jQuery('#Contact0PostalCode3').val(location.zipcode);
			jQuery('#Contact0Country3').val(location.country_name);
			jQuery('#Contact0TimeZone').val(location.time_zone);
			jQuery('#Contact0_IPAddress').val(location.ip);
			}
	} );
});
</script>

<input name="Contact0City3" id="Contact0City3" type="hidden" value="" />
<input name="Contact0State3" id="Contact0State3" type="hidden" value="" />
<input name="Contact0PostalCode3" id="Contact0PostalCode3" type="hidden" value="" />
<input name="Contact0Country3" id="Contact0Country3" type="hidden" value="" />
<input name="Contact0TimeZone" id="Contact0TimeZone" type="hidden" value="" />
<input name="Contact0_IPAddress" id="Contact0_IPAddress" type="hidden" value="" />