<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ajax | Jörg Drzycimski</title>
	<atom:link href="https://drzycimski.com/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>https://drzycimski.com/tag/ajax/</link>
	<description></description>
	<lastBuildDate>Sun, 01 Oct 2017 16:54:57 +0000</lastBuildDate>
	<language>de</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>Google Maps Geocoding with JSON and PHP</title>
		<link>https://drzycimski.com/programmierung/google-maps-geocoding-api-with-json-and-php/</link>
					<comments>https://drzycimski.com/programmierung/google-maps-geocoding-api-with-json-and-php/#comments</comments>
		
		<dc:creator><![CDATA[Jörg]]></dc:creator>
		<pubDate>Sat, 29 Jan 2011 11:13:20 +0000</pubDate>
				<category><![CDATA[Programmierung]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[Geocoding]]></category>
		<category><![CDATA[Google API v3]]></category>
		<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<guid isPermaLink="false">http://www.drzycimski.com/?p=115</guid>

					<description><![CDATA[<p>When using maps on a website, Google is clearly the service to go with. If you are using JavaScript only, parsing a JSON response from Google shouldn´t be difficult, but how to use the response in PHP? In this article, I´ll describe how to use Zend Framework components, but with a link to How-To´s with [&#8230;]</p>
<p>The post <a href="https://drzycimski.com/programmierung/google-maps-geocoding-api-with-json-and-php/">Google Maps Geocoding with JSON and PHP</a> appeared first on <a href="https://drzycimski.com">Jörg Drzycimski</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>When using maps on a website, Google is clearly the service to go with. If you are using JavaScript only, parsing a JSON response from Google shouldn´t be difficult, but how to use the response in PHP? In this article, I´ll describe how to use Zend Framework components, but with a link to How-To´s with standard PHP functions, to</p>
<ol>
<li>Send a request to Google Maps API</li>
<li>Get the response in JSON format (no problem to switch to XML)</li>
<li>Parse the response from Google to a more readable format</li>
<li>And finally, use those values in a PHP script</li>
</ol>
<p>I needed the response for the meta geotags on <a title="surfspot.de" href="http://www.surfspot.de" target="_blank" rel="noopener">surfspot.de</a>, where I use a lot of maps to show the locations of&#8230; surfspots, obviously. The geotag for a spot would be something like this:</p>
<pre class="brush:html">&lt;meta name="geo.region" content="COUNTRY_SHORT-REGION_SHORT" /&gt;
&lt;meta name="geo.placename" content="CITY" /&gt;
&lt;meta name="geo.position" content="LATITUDE;LONGITUDE" /&gt;
&lt;meta name="ICBM" content="LATITUDE, LONGITUDE" /&gt;
</pre>
<p>&#8230; with latitude and longitude known (stored in database) , and city, region and country to find out from Google. Pls find details about HTML geotags on <a title="HTML Geotag on Wikipedia" href="http://en.wikipedia.org/wiki/Geotagging" target="_blank" rel="noopener">Wikipedia</a>. The meaning of the ICBM tag is just hilarious 😀 For details on Google Geocoding, pls refer to their <a title="Google documentation" href="http://code.google.com/intl/en/apis/maps/documentation/geocoding/" target="_blank" rel="noopener">documentation</a>.</p>
<p>Fist, we need the class to query Google:</p>
<pre class="brush:php">class JD_Geocoder_Request
{
 /**
 * @class vars
 */

 // Google´s geocode URL
 public $url = 'http://maps.google.com/maps/api/geocode/json?';

 // Params for request
 public $sensor       = "false"; // REQUIRED FOR REQUEST!
 public $language     = "en";

 // Class vars
 public $response     = '';
 public $country_long = '';
 public $country_short= '';
 public $region_long  = '';
 public $region_short = '';
 public $city         = '';
 public $address      = '';
 public $lat          = '';
 public $lng          = '';
 public $location_type= '';

 /**
 * Constructor
 *
 * @param mixed $config
 * @return void
 */
 public function __construct($config = null)
 {

 }

} // end class
</pre>
<p>I prefer to store all results I may or may not need in class vars, so I can just echo them out.</p>
<p>Second, we need to know what kind of search we need to perform, forward geocoding with an address (say, from a form), or a reverse geocoding search, using lat and lng for the query. I implemented both methods, but pls be aware I didn´t test the forward search&#8230; since I don´t need it yet 😉</p>
<pre class="brush:php">/**
 * Forward search: string must be an address
 *
 * @param string $address
 * @return obj $response
 */
 public function forwardSearch($address)
 {
   return $this-&gt;_sendRequest("address=" . urlencode(stripslashes($address)));
 } // end forward

 /**
 * Reverse search: string must be latitude and longitude
 *
 * @param float $lat
 * @param float $lng
 * @return obj $response
 */
 public function reverseSearch($lat, $lng)
 {
   return $this-&gt;_sendRequest("latlng=" . (float) $lat . ',' . (float) $lng);
 } // end reverse
</pre>
<p>Both methods handle formatting the parameters, as well. If you want to handle just the response object, you can skip ahead and ignore the next functions. As said earlier, I like to store the response in class vars in order to just echo them out, or store them in a database. The two functions set the defaults I need to know, and search for other values as well. Pls note that only the response object $address_components is used for the search, Google may or may not return a lot more &#8211; check their docs for this.</p>
<pre class="brush:php">/**
 * Search Address Components Object
 *
 * @param string $type
 * @return object / false
 */
 public function searchAddressComponents($type) {
   foreach($this-&gt;response-&gt;results[0]-&gt;address_components as $k=&gt;$found){
     if(in_array($type, $found-&gt;types)){
       return $found;
     }
   }
   return false;
 }

/**
 * Parse JSON default values: map object values to readable content
 *
 * @param none
 * @return none
 */
 private function _setDefaults()
 {
   $country = $this-&gt;searchAddressComponents("country");
   $this-&gt;country_long    = $country-&gt;long_name;
   $this-&gt;country_short    = $country-&gt;short_name;
   $region = $this-&gt;searchAddressComponents("administrative_area_level_1");
   $this-&gt;region_long = $region-&gt;long_name;
   $this-&gt;region_short    = $region-&gt;short_name;
   $city = $this-&gt;searchAddressComponents("locality");
   $this-&gt;city    = $city-&gt;short_name;
   $this-&gt;address = $this-&gt;response-&gt;results[0]-&gt;formatted_address;
   $this-&gt;lat = $this-&gt;response-&gt;results[0]-&gt;geometry-&gt;location-&gt;lat;
   $this-&gt;lng = $this-&gt;response-&gt;results[0]-&gt;geometry-&gt;location-&gt;lng;
   $this-&gt;location_type = $this-&gt;response-&gt;results[0]-&gt;geometry-&gt;location_type;
 } // end set
</pre>
<p>The JSON response from Google is likely to change, so you need to control the mapping every now and then. If you use the reverse geocoding, pls note that lat and lng may NOT be the same values you used to call the search with, but refer to the next best address, even if location_type is returned as &#8222;rooftop&#8220;. This doesn´t matter if you search on land, but surfspots might be 500m down a beach from a known location, at least, known to Google.</p>
<p>Now for the slightly more difficult part&#8230; the actual request to Google Maps API, and parsing the JSON response. Using the Zend Framework, sending a request to Google is a tad bit easier than using pure PHP. If you don´t use ZF and/or use XML response format, check <a title="phpRiot article" href="http://www.phpriot.com/articles/google-maps-geocoding" target="_blank" rel="noopener">this article</a> on phpRiot, and replace the $client code.</p>
<pre class="brush:php">/**
 * Send Google geocoding request
 *
 * @param string $search
 * @return object response (body only)
 */
 private function _sendRequest($search)
 {
   $client = new Zend_Http_Client();
   $client-&gt;setUri($this-&gt;url . $search . '&amp;language=' . strtolower($this-&gt;language) . '&amp;sensor=' . strtolower($this-&gt;sensor));
   $client-&gt;setConfig(array(
     'maxredirects' =&gt; 0,
     'timeout'      =&gt; 30));
   $client-&gt;setHeaders(array(
     'Accept-encoding' =&gt; 'json',
     'X-Powered-By' =&gt; 'Zend Framework GEOCMS by Joerg Drzycimski'));
   $response = $client-&gt;request();
   $body = $response-&gt;getBody();
   $this-&gt;response = Zend_Json::decode($body, Zend_Json::TYPE_OBJECT);
   if ($this-&gt;response-&gt;status == "OK") {
     // set some default values for reading
     $defaults = $this-&gt;_setDefaults();
     return $this-&gt;response;
   } else {
     echo "Geocoding failed, server responded: " . $this-&gt;response-&gt;status;
     return false;
   }
 } // end request
</pre>
<p>Now that you have the request up and running, the code requirde to generate the meta geotags is quite simple:</p>
<pre class="brush:php">$georequest = new JD_Geocoder_Request();
$lat = '54.424899';
$lng = '11.096671';
$georequest-&gt;reverseSearch($lat,$lng);
echo '&lt;meta name="geo.region" content="' . $georequest-&gt;country_short . '-' . $georequest-&gt;region_short . '" /&gt;';
echo '&lt;meta name="geo.placename" content="' . $georequest-&gt;city . '" /&gt;';
echo '&lt;meta name="geo.position" content="' . $lat . ';' . $lng . '" /&gt;';
echo '&lt;meta name="ICBM" content="' . $lat . ',' . $lng . '" /&gt;';
</pre>
<p>Links: <a href="https://drzycimski.com/wp-content/uploads/2011/01/Request.zip">Class File</a> as ZIP.</p>
<p>The post <a href="https://drzycimski.com/programmierung/google-maps-geocoding-api-with-json-and-php/">Google Maps Geocoding with JSON and PHP</a> appeared first on <a href="https://drzycimski.com">Jörg Drzycimski</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://drzycimski.com/programmierung/google-maps-geocoding-api-with-json-and-php/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
		<item>
		<title>No-MVC Zend Framework: jQuery Ajax receiver / Part V</title>
		<link>https://drzycimski.com/programmierung/no-mvc-zend-framework-jquery-ajax-receiver-part-v/</link>
					<comments>https://drzycimski.com/programmierung/no-mvc-zend-framework-jquery-ajax-receiver-part-v/#respond</comments>
		
		<dc:creator><![CDATA[Jörg]]></dc:creator>
		<pubDate>Tue, 11 May 2010 16:34:31 +0000</pubDate>
				<category><![CDATA[Programmierung]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Star Rating]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<guid isPermaLink="false">http://blog.drzycimski.com/?p=64</guid>

					<description><![CDATA[<p>The jQuery JavaScript is up and running, but needs a PHP file to handle the Ajax data submitted by the rating script. This will be done with PHP in the receiver you defined in your JavaScript, named &#8222;rating.php&#8220; in the &#8222;public/includes&#8220; folder. It&#8217;s important to put this PHP file in your public folder, because if [&#8230;]</p>
<p>The post <a href="https://drzycimski.com/programmierung/no-mvc-zend-framework-jquery-ajax-receiver-part-v/">No-MVC Zend Framework: jQuery Ajax receiver / Part V</a> appeared first on <a href="https://drzycimski.com">Jörg Drzycimski</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>The jQuery JavaScript is up and running, but needs a PHP file to handle the Ajax data submitted by the rating script. This will be done with PHP in the receiver you defined in your JavaScript, named &#8222;rating.php&#8220; in the &#8222;public/includes&#8220; folder. It&#8217;s important to put this PHP file in your public folder, because if you pointed your webserver to &#8222;public&#8220;, anything outside (subfolders of  your document root) can&#8217;t be accessed by JavaScript.</p>
<p>Create the &#8222;rating.php&#8220;, and add the following lines:</p>
<pre class="brush:php">&lt;?php
require_once('bootstrap.php'); // Setup Zend Framework Environment
header("Cache-Control: no-cache");
$rating = new Mylib_Rating_Controller();
$score = $rating-&gt;setRatingById($_GET['id'], $_GET['val']); // rate object and get scores
echo Zend_Json::encode($score); // send response array in JSON format
?&gt;
</pre>
<p>The <code>setRatingById</code> passes the values from your JavaScript to the (soon to be created) controller, and receives the updated values from your (soon to be created) database. When I started out using jQuery in combination with Ajax, I was kind of afraid to find it difficult&#8230; but it really<em> is </em>that easy 😉</p>
<p>Next up: The rating controller</p>
<p>The post <a href="https://drzycimski.com/programmierung/no-mvc-zend-framework-jquery-ajax-receiver-part-v/">No-MVC Zend Framework: jQuery Ajax receiver / Part V</a> appeared first on <a href="https://drzycimski.com">Jörg Drzycimski</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://drzycimski.com/programmierung/no-mvc-zend-framework-jquery-ajax-receiver-part-v/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>No-MVC Zend Framework: jQuery Star Rating code / Part IV</title>
		<link>https://drzycimski.com/programmierung/no-mvc-zend-framework-jquery-star-rating-code-part-iv/</link>
					<comments>https://drzycimski.com/programmierung/no-mvc-zend-framework-jquery-star-rating-code-part-iv/#comments</comments>
		
		<dc:creator><![CDATA[Jörg]]></dc:creator>
		<pubDate>Fri, 07 May 2010 16:35:58 +0000</pubDate>
				<category><![CDATA[Programmierung]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Star Rating]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<guid isPermaLink="false">http://blog.drzycimski.com/?p=48</guid>

					<description><![CDATA[<p>Now for the easy part: creating the page for our jQuery Star Rating plugin. It&#8217;s going to be just a bit of HTML, JavaScript with Ajax components, and PHP. If you want to integrate the Star Rating in an existing page, you can just copy the relevant parts into any HTML file. The JavaScript Before [&#8230;]</p>
<p>The post <a href="https://drzycimski.com/programmierung/no-mvc-zend-framework-jquery-star-rating-code-part-iv/">No-MVC Zend Framework: jQuery Star Rating code / Part IV</a> appeared first on <a href="https://drzycimski.com">Jörg Drzycimski</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Now for the easy part: creating the page for our jQuery Star Rating plugin. It&#8217;s going to be just a bit of HTML, JavaScript with Ajax components, and PHP. If you want to integrate the Star Rating in an existing page, you can just copy the relevant parts into any HTML file.</p>
<p><strong>The JavaScript</strong></p>
<p>Before continuing (or if this doesn&#8217;t work for you), you might want to familiarize yourself with the Star Rating plugin at <a href="http://zensoftware.org/archives/483" target="_blank" rel="noopener">http://zensoftware.org/archives/483</a>.</p>
<p>Add the following lines to the HEAD section of your &#8222;index.php&#8220;. This assumes that you already have one&#8230; if not, use you standard HTML template, and don&#8217;t forget to include your bootstrap 😉</p>
<pre class="brush:js">&lt;script type="text/javascript" src="/js/jquery/js/jquery-1.4.2.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="/js/rating/jquery.rating.js"&gt;&lt;/script&gt;
&lt;link type="text/css" href="/js/rating/jquery.rating.css" rel="stylesheet" media="screen" /&gt;

&lt;script type="text/javascript"&gt;
$(function(){

 $(".rating")
 .rating({"showCancel": false})
 .bind("change", function(){
   var id = $(this).attr("id");
   var rate = "id=" + id + "&amp;val=" + $(this).val();
   var loading = 'Loading';
   // Or use image instead, looks prettier ;-)
   // var loading = '&lt;img src="/images/loading.gif" /&gt;';
   $.ajax({
     type: "GET",
     url: "/includes/rating.php",
     dataType: "json",
     data: rate,
     timeout: 10000,
     beforeSend: function(){
       $("#rating_value_" + id).html(loading);
     },
     success: function(response){
       $("#rating_value_" + id).html(response.average + " points");
     },
     error: function(){
       $("#rating_value").html("Error!");
     }
    }); // end ajax
  }); // end rating / bind

}); // end $()
&lt;/script&gt;
</pre>
<p>I used &#8222;loading&#8220; as a variable (as opposed to the direct output of &#8222;Error!&#8220;). You can Google the standard Ajax loading image, and put the HTML image tag in that variable to pretty things up, instead of that boring message.</p>
<p>BTW, I always use a leading slash and the complete path in relation to the root directory (e.g. &#8222;/images&#8220;) before each image, JS and CSS file. This way, I don&#8217;t have to worry about broken links when I split header and content sections into different files, and move them to different directories&#8230; which I favorize, because header sections are the same throughout the website, whereas content sections might use different templates.</p>
<p><strong>The PHP / HTML code</strong></p>
<p>You can put this code anywhere within your BODY section of your &#8222;index.php&#8220;, even in existing pages&#8216; DIVs:</p>
<pre class="brush:php">&lt;?php
$id = 1;
$rating = new Mylib_Rating_Controller();
echo $rating-&gt;getRatingView($id);
?&gt;
</pre>
<p>Set the variable &#8222;$id&#8220; to any unique id you want to rate. On my website <a href="http://www.surfspot.de" target="_blank" rel="noopener">surfspot.de</a>, I use it to rate a page, so the id comes from my database, and represents the page id. If you have more than one rating on one page, say for a couple of images, instantiate the class once, and only use <code>echo $rating-&gt;getRatingView($id)</code> with a different id for each image.</p>
<p>Next up: The jQuery Ajax receiver</p>
<p>The post <a href="https://drzycimski.com/programmierung/no-mvc-zend-framework-jquery-star-rating-code-part-iv/">No-MVC Zend Framework: jQuery Star Rating code / Part IV</a> appeared first on <a href="https://drzycimski.com">Jörg Drzycimski</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://drzycimski.com/programmierung/no-mvc-zend-framework-jquery-star-rating-code-part-iv/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
		<item>
		<title>No-MVC Zend Framework: Installing ZF &#038; jQuery / Part II</title>
		<link>https://drzycimski.com/programmierung/zend-framework-without-mvc-part-ii/</link>
					<comments>https://drzycimski.com/programmierung/zend-framework-without-mvc-part-ii/#respond</comments>
		
		<dc:creator><![CDATA[Jörg]]></dc:creator>
		<pubDate>Sun, 02 May 2010 11:34:30 +0000</pubDate>
				<category><![CDATA[Programmierung]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Dojo]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQueryUI]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Star Rating]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<guid isPermaLink="false">http://blog.drzycimski.com/?p=36</guid>

					<description><![CDATA[<p>My tutorial of utilizing Zend Framework without MVC will be a jQuery rating controller, with ZF used to query the database, spit out the view (the actual stars), and handle rating changes thru Ajax, updating the DB. Though I prefer not to use ZF as an application, I tried to program as close to the [&#8230;]</p>
<p>The post <a href="https://drzycimski.com/programmierung/zend-framework-without-mvc-part-ii/">No-MVC Zend Framework: Installing ZF &#038; jQuery / Part II</a> appeared first on <a href="https://drzycimski.com">Jörg Drzycimski</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>My tutorial of utilizing Zend Framework without MVC will be a jQuery rating controller, with ZF used to query the database, spit out the view (the actual stars), and handle rating changes thru Ajax, updating the DB. Though I prefer not to use ZF as an application, I tried to program as close to the MVC structure as possible. If you&#8217;re using ZF already, you will see that my way of utilizing it is close to a ZF plugin resource &#8211; with some minor changes, you&#8217;ll be able to adapt it into the framework in no time at all.</p>
<p><strong>Installing Zend Framework</strong></p>
<p>On your server, add some directories to accomodate ZF, jQuery, and your own PHP files. Assuming that you start from scratch, you need to point your webserver to the &#8222;public&#8220; directory, instead of  &#8222;document root&#8220;. This way, one can&#8217;t access your lib files, e.g. configs, by URL.</p>
<pre>&gt; document root
  &gt; library
    &gt; Mylib
    &gt; Zend
  &gt; public
     &gt; images
     &gt; includes
     &gt; js
       &gt; jquery</pre>
<p>Download the lastest ZF version at <a href="http://framework.zend.com/download/latest" target="_blank" rel="noopener">http://framework.zend.com/download/latest</a>, and unpack the files. Get the content of the &#8222;library/Zend&#8220; form your archive, and copy it into the &#8222;library/Zend&#8220; folder on your server. Next, get the latest of jQueryUI at <a href="http://jqueryui.com/download" target="_blank" rel="noopener">http://jqueryui.com/download</a>, and put the content in the &#8222;jquery&#8220; folder. jQueryUI is just a nice JavaScript framework (that word again&#8230;) with widgets and effects&#8230; you might need them later 😉</p>
<p>A short note: though ZF supports the <a href="http://www.dojotoolkit.org" target="_blank" rel="noopener">Dojo</a> JavaScript framework, I personally prefer jQuery. Dojo definetly has the cooler widgets, but the support sucks&#8230; no forum to speak of, and mailing list support slow if ever. If you run into problems with Dojo, you&#8217;re basically on your own&#8230; the exact opposite of jQuery, where you 1. get support within hours, and 2. rarely run into problems to begin with 😉 Just my 2 cents&#8230;</p>
<p>Anyway, the part missing for our little tutorial is the Star Rating plugin for jQuery. Get it at <a href="http://zensoftware.org/archives/483" target="_blank" rel="noopener">http://zensoftware.org/archives/483</a>, and put it in your &#8222;js&#8220; folder, into a &#8222;rating&#8220; subdirectory.</p>
<p>Next up: Bootstrapping ZF</p>
<p>The post <a href="https://drzycimski.com/programmierung/zend-framework-without-mvc-part-ii/">No-MVC Zend Framework: Installing ZF &#038; jQuery / Part II</a> appeared first on <a href="https://drzycimski.com">Jörg Drzycimski</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://drzycimski.com/programmierung/zend-framework-without-mvc-part-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
