<?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>designing4u.de &#187; ajax</title>
	<atom:link href="http://www.designing4u.de/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designing4u.de</link>
	<description>Yet Another Coding Blog</description>
	<lastBuildDate>Fri, 29 Jul 2011 08:11:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Google Maps and jQuery &#8211; loading markers when user moves the map</title>
		<link>http://www.designing4u.de/2009/02/google-maps-and-jquery-loading-markers-when-user-moves-the-map/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=google-maps-and-jquery-loading-markers-when-user-moves-the-map</link>
		<comments>http://www.designing4u.de/2009/02/google-maps-and-jquery-loading-markers-when-user-moves-the-map/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 15:28:56 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[add marker]]></category>
		<category><![CDATA[ajax]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=31</guid>
		<description><![CDATA[Recently I was working on a project, in which the goal was to display all company branches within a certain distance, defined by a zip code and a range provided by user input. The solution was pretty simple. A user provides a zip code and the distance range, the script translates the zip code into [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was working on a project, in which the goal was to display all company branches within a certain distance, defined by a zip code and a range provided by user input. The solution was pretty simple. A user provides a zip code and the distance range, the script translates the zip code into geographical length and by using the Haversine formula retrieves all branches from the database and displays them in a Google map. This script was only working when a user submitted the information through a form that was provided for this script. As soon as he or she started to play with the Google map, the information about company's branches outside the initial zip code entered were not updated. When one assumes that a company has for instance 1000 Branches and resides in a small country, displaying other branches, while a user is moving within the Google map may be crucial to their search. I had to come up with an idea, how to solve this problem in my jQuery code. This is my solution.<br />
<span id="more-31"></span><br />
First of all, I had to decide how to handle this event. One possibility was to write my own event listener and trigger it using GEvent.trigger() method. One of the pre-build events provided "out of the box" by Google Map API is "moveend". Using this event, we can send an AJAX request every time a user moves the map, either when he or she drags and drops it or he or she is using provided controls. This approach has definite disadvantages because it might be possible that a user will play with a map a lot and this will lead to several AJAX requests every time he or she does that. On the other hand, you also need to assume that since a user is using the form you provided him or her in the first place, this situation probably won't be that common.</p>
<p>One thing we need to make a decision about before we will start coding is when we should use the Haversine formula and when we should display the markers according to the borders of the map. According to <a href="http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL" target="_blank">this study</a>, using the Haversine formula in a web query on a MySQL database with 800k entries might take up to 8 seconds. Therefore it will be crucial to split our jQuery code into requests concerning retrieving the data based on the Haversine formula and data within the borders of the map. In this example I won't give a complete walk through of how to measure the distance between two points (it will probably be a good subject for another example). In this example I will only show how to display data on an "moveend" event.</p>
<p>Let's take a look at the code. In line 1, we are executing the code after the document is loaded. I would consider it a standard procedure in writing unobtrusive JavaScript. In line 2, we define our map container, which corresponds with a HTML tag whose id equals map. In line 3, we are checking if the user’s browser is compatible with Google Map API and if it is, and our map container also exists, we can finally execute the rest of the code.</p>
<p>In line 5, we are instantiating our Google map and in lines 6 and 7 we are defining some controls for our map, the center (GLatLng) and zoom level (14). In line 9, we would have our code to measure the distance from given zip code and to display the markers on our map. The most interesting part of our code starts in line 14. We add a listener, which triggers on "moveend" event. In line 15, we use the getBounds() method, which returns GLatLngBounds object for us. In lines 16 to 19, we use getSouthWest() and getNorthEast() methods of our GLatLngBounds objects. This methods return GLatLng object, therefore we can directly use its methods to retrieve the geographical lengths of our south west and northeast corner. The last part is pretty simple, and I will leave it to you to adjust it to your needs. In line 20, we use the AJAX request, and we pass all geographical lengths to our php file. We assume that the response will be a valid json_encode() representation of the data retrieved from the database, and in lines 21 to 23, we add our markers to the map. </p>
<p>Here is the code you have been waiting for:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
  <span style="color: #003366; font-weight: bold;">var</span> mapContainer <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#map'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>GBrowserIsCompatible<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>mapContainer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMap2<span style="color: #009900;">&#40;</span>mapContainer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">addControl</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLargeMapControl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">52.519015140666</span><span style="color: #339933;">,</span><span style="color: #CC0000;">13.419671058654785</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">14</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#submit'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// here goes the code handling calculatig of distance</span>
        <span style="color: #006600; font-style: italic;">// truncated because it is not a part of this post</span>
&nbsp;
        GEvent.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>map<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;moveend&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #003366; font-weight: bold;">var</span> bounds <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getBounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> swLat <span style="color: #339933;">=</span> bounds.<span style="color: #660066;">getSouthWest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">lat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> swLng <span style="color: #339933;">=</span> bounds.<span style="color: #660066;">getSouthWest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">lng</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> neLat <span style="color: #339933;">=</span> bounds.<span style="color: #660066;">getNorthEast</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">lat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> neLng <span style="color: #339933;">=</span> bounds.<span style="color: #660066;">getNorthEast</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">lng</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'helper.php'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> swLat<span style="color: #339933;">:</span> swLat<span style="color: #339933;">,</span> swLng<span style="color: #339933;">:</span> swLng<span style="color: #339933;">,</span> neLat<span style="color: #339933;">:</span> neLat<span style="color: #339933;">,</span> neLng<span style="color: #339933;">:</span> neLng <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> point <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">lat</span><span style="color: #339933;">,</span> data.<span style="color: #660066;">lng</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            map.<span style="color: #660066;">addOverlay</span><span style="color: #009900;">&#40;</span>marker<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You might ask why we are placing this code inside of jQuery's click event? The answer is simple. This way we can prevent triggering the "moveend" event, before a user uses the form. Only after he or she uses the form, our listener will react to the user's interaction with the map.</p>
<p>Another question might be, why we are not using GBounds.extend() method to adjust the zoom level? An answer for this question is as simple as the last one. We already defined the zoom level by passing the geographical lengths of our rectangle to the php file and since we triggered "moveend" event, we always have an actual part of the map, which a user is seeing at the point of triggering it.</p>
<p>I hope you will like this example. I did not want to provide the whole code for this example, because first of all I wanted to split it into two entries and second of all, I did not want to provide an example which you can copy and past, but rather give a solution for a certain problem and possibility start a discussion about doing it better. Have fun with this one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2009/02/google-maps-and-jquery-loading-markers-when-user-moves-the-map/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Maps, jQuery and XML &#8211; saving markers with user input</title>
		<link>http://www.designing4u.de/2008/08/google-maps-jquery-and-xml-saving-markers-with-user-input/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=google-maps-jquery-and-xml-saving-markers-with-user-input</link>
		<comments>http://www.designing4u.de/2008/08/google-maps-jquery-and-xml-saving-markers-with-user-input/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 08:59:54 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[saving markers]]></category>
		<category><![CDATA[user input]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=27</guid>
		<description><![CDATA[I just got a first request from a user of my site to help him with his problem. Let me describe it in detail. Assume that a user is visiting your site and he or she wants to place a marker on a google map displayed on your page. Additionally, we want to get some [...]]]></description>
			<content:encoded><![CDATA[<p>I just got a first request from a user of my site to help him with his problem. Let me describe it in detail. Assume that a user is visiting your site and he or she wants to place a marker on a google map displayed on your page. Additionally, we want to get some input from our user, therefore after he or she clicks on the google map we will provide an info could with a form. Our user will be able to provide a name, a message and a link and submit it. I didn't want to make this example too complicated, therefore handling the links or uploading the files you will have to solve by yourself. Let's move to the code.<br />
<span id="more-27"></span><br />
Our final solution will look more or less like <a title="Google Maps, jQuery and XML - saving markers and user input" href="http://www.designing4u.de/examples/marker-gmap/index.php" target="_blank">this</a>.</p>
<p>In our example we will use two file types - PHP and XML. Our PHP file will be responsible for saving the user input in XML file and displaying the google map with all saved markers. Our XML file will be basically a place, where we will save all the markers saved by an user. It is worth to mention that you can easily replace the XML file with any kind of a database. If you do so, you will for example have possibility to approve each marker saved on your page. Additionally, if you have some kind of authentication system on your site, you can also save according username of a person who placed a marker on your site and provide some kind of statistics for your users. It's up to you, how you will use this code.</p>
<p>After some initial explanation we can move to our PHP file. Let's start with the header of our file:</p>
<p>1. PHP file</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$fh</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'markers.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  try <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000088;">$marker</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'marker'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$marker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lat'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lat'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$marker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lng'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lng'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$marker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'msg'</span><span style="color: #339933;">,</span> <span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'msg'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$marker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span> <span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$marker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'link'</span><span style="color: #339933;">,</span> <span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'markers.xml'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">saveXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location:index.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We use PHP function file_get_contents() to load the content of our XML file. Our XML file would be an empty XML file with markers element, which will be a holder for our markers. We initialize a PHP build in class SimpleXMLElement with our file and catch an exception if any error occur. If our user clicks on a save button (the code of this form will follow) we create a child element with according attributes and then save it as a new XML file.</p>
<p>This way we will add new markers to our XML file without loosing any previous information saved in our XML file. After our file is saved we redirect our user to the index page to prevent reloading of the page and saving multiple markers with the same information. I know it is not the cleanest way to do that, but it is just an example which should serve as a blueprint for your code.</p>
<p>You can notice that I use utf8_encode() function to encode our user input into correct charset. Because I encountered major problems with foreign characters and our final solution should work for standard characters as well as for special characters, that is a necessary step to handle this problem. The only thing you should keep in mind is to set the charset of your site to UTF-8.</p>
<p>Don't forget to put that code before the head section of your PHP file, because otherwise you will get an error that the header were already send.</p>
<p>Let's take a look at our jQuery function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> mark<span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> pointA<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>GBrowserIsCompatible<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> m <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#map&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #003366; font-weight: bold;">var</span> map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMap2<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> start <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">63.13450320833446</span><span style="color: #339933;">,</span><span style="color: #CC0000;">16.69921875</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> zoomLevel <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span>start<span style="color: #339933;">,</span> zoomLevel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">addControl</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GSmallMapControl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'markers.xml'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'marker'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          <span style="color: #003366; font-weight: bold;">var</span> lat    <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lat'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> lng    <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lng'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> html   <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #339933;">;</span>
          html      <span style="color: #339933;">+=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'msg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #339933;">;</span>
          html      <span style="color: #339933;">+=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'link'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> point  <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>lat<span style="color: #339933;">,</span>lng<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          map.<span style="color: #660066;">addOverlay</span><span style="color: #009900;">&#40;</span>marker<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          GEvent.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>marker<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            marker.<span style="color: #660066;">openInfoWindowHtml</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      GEvent.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>map<span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>overlay<span style="color: #339933;">,</span> point<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>mark<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          map.<span style="color: #660066;">removeOverlay</span><span style="color: #009900;">&#40;</span>mark<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          pointA <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GPoint<span style="color: #009900;">&#40;</span>point.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> point.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          mark <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>pointA<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          map.<span style="color: #660066;">addOverlay</span><span style="color: #009900;">&#40;</span>mark<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          map.<span style="color: #660066;">getCenter</span><span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> lat <span style="color: #339933;">=</span> point.<span style="color: #660066;">y</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> lng <span style="color: #339933;">=</span> point.<span style="color: #660066;">x</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> form <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;
          &lt;form action=&quot;</span>\<span style="color: #3366CC;">&quot;index.php<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> method<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>post<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #3366CC;">&quot; +
          &quot;</span><span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>lat<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>hidden<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">+</span>lat<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">/&gt;</span><span style="color: #3366CC;">&quot;+
          &quot;</span><span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>lng<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>hidden<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">+</span>lng<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">/&gt;</span><span style="color: #3366CC;">&quot;+
          &quot;</span><span style="color: #000066;">Name</span><span style="color: #339933;">:</span>
          <span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>name<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>text<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">/&gt;</span><span style="color: #3366CC;">&quot;+
          &quot;</span>Msg<span style="color: #339933;">:</span>
          <span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>msg<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>text<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">/&gt;</span><span style="color: #3366CC;">&quot;+
          &quot;</span>Link<span style="color: #339933;">:</span>
          <span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>link<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>text<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">/&gt;</span><span style="color: #3366CC;">&quot;+
          &quot;</span><span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>submit<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>submit<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>save<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">/&gt;</span><span style="color: #3366CC;">&quot;+
          &quot;</span><span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span><span style="color: #3366CC;">&quot;;
          map.openInfoWindowHtml(point,form);
        }
      });
    }
  }
});</span></pre></td></tr></table></div>

<p>In the first part we declare two variables pointA and mark which will later serve to retrieve the longitude and latitude of our marker. We will also reduce the ability of a user to place several markers at the same time.</p>
<p>We check if our browser is compatible with google maps and we look for a place holder for our map. If both of the conditions are fulfilled we display our map and use AJAX request to display all the markers saved in our XML file. We also add an event listener which is triggered every time a user clicks on an existing marker. If it is a case, we display an info cloud with information saved by the user.</p>
<p>In the second part of our function we add another event listener, which is triggered every time a user places a new marker. In our info cloud we will display a form which I mentioned before and save the longitude and latitude in hidden fields. After our user clicks a save button the information from the form will be processed by our PHP file which will create another marker and display it on our google map.</p>
<p>The last part is our XML file:</p>
<p>2. XML file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;markers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;marker</span> <span style="color: #000066;">lat</span>=<span style="color: #ff0000;">&quot;64.4348920430406&quot;</span> <span style="color: #000066;">lng</span>=<span style="color: #ff0000;">&quot;15.205078125&quot;</span> <span style="color: #000066;">msg</span>=<span style="color: #ff0000;">&quot;Msg&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Name&quot;</span> <span style="color: #000066;">link</span>=<span style="color: #ff0000;">&quot;yes!&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/markers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>As I said before this example is just a blueprint and should be extended by user input validation. As I also said before, you don't really need to use XML file and you can also implement an approval system of each marker placed on your site. You will have to save your information in a database though. You can use my post about <a title="google maps jquery and xml displaying a simple navigation" href="http://www.designing4u.de/2008/05/google-maps-jquery-and-xml-displaying-a-simple-navigation/">displaying a simple navigation</a> to get the idea, how to create an XML file based on information you retrieve from database. I hope this will be helpful. I'm waiting for your feedback.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/08/google-maps-jquery-and-xml-saving-markers-with-user-input/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>jQuery drop down &#8211; loading content according to option</title>
		<link>http://www.designing4u.de/2008/05/jquery-drop-down-loading-content-according-to-option/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jquery-drop-down-loading-content-according-to-option</link>
		<comments>http://www.designing4u.de/2008/05/jquery-drop-down-loading-content-according-to-option/#comments</comments>
		<pubDate>Fri, 23 May 2008 15:36:47 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[drop down]]></category>
		<category><![CDATA[option]]></category>
		<category><![CDATA[select]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=19</guid>
		<description><![CDATA[It was a really busy day for me today, so I came up with a simple code for displaying content according to the option our user chooses from drop down list. What we will try to accomplish is to provide a user a drop down list and load content from a remote PHP file using [...]]]></description>
			<content:encoded><![CDATA[<p>It was a really busy day for me today, so I came up with a simple code for displaying content according to the option our user chooses from drop down list. What we will try to accomplish is to provide a user a drop down list and load content from a remote PHP file using AJAX request. Additionally we will filter the data and display only the content relevant to the chosen option. We will also make use of chainability of jQuery and add a nice fade in effect after the text is loaded.<br />
<span id="more-19"></span><br />
<!--adsensestart--></p>
<p>After we'll be done with our example, the final result should look more or less like that:<br />
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script><br />
<script type="text/javascript">
	$(document).ready(function(){
		$('#select').change(function() {
			var option = $(this).val();
			$.get('/examples/dropdown/select.php', {select:option}, function(data) {
				$('#result').html(data).hide().fadeIn(1000);
			});
		});
	});
</script></p>
<select name="select" id="select">
<option value="">Select</option>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
<option value="option4">Option 4</option>
<option value="option5">Option 5</option>
</select>
<div style="height:35px;">
<div style="border:1px solid #000;padding:10px;color:#ff0000;display:none;" id="result"></div>
</div>
<p>As usually don't forget to include jQuery library in the head section of your document. Let's take a closer look at the jQuery code we will use in this example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#select'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> option <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'select.php'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>select<span style="color: #339933;">:</span>option<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#result'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>I think I don't need to remind anyone that you should place this code also in the head section of your document. Our goal is to write unobtrusive code though. </p>
<p>Our code is really simple this time. After the DOM is loaded we add an event listener, which will be triggered on onchange event of our drop down list with id select. If the event was triggered we save the value of chosen option in 'option' variable and use the AJAX request to get the content of the remote file, which in our case is select.php, which should be placed in the same directory. Additionally we pass an argument, which in our case is the value of the chosen option. We then use html() to past retrieved data into our div container with id result, we hide it and use fadeIn() effect to display it in a nicer way.</p>
<p>Here is the HTML code you should put in the body section of your document:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;select&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;select&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span>&gt;</span>Select<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;option1&quot;</span>&gt;</span>Option 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;option2&quot;</span>&gt;</span>Option 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;option3&quot;</span>&gt;</span>Option 3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;option4&quot;</span>&gt;</span>Option 4<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;option5&quot;</span>&gt;</span>Option 5<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;result&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;border:1px solid #000;padding:10px;color:#ff0000;display:none;&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>I don't think this code needs any further explanation. Probably, what's more important, is the content of the php file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'select'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'option1'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'the option you have chosen is 1'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'select'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'option2'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'the option you have chosen is 2'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'select'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'option3'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'the option you have chosen is 3'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'select'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'option4'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'the option you have chosen is 4'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'select'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'option5'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'the option you have chosen is 5'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Basically we display the content according to the data passed in our AJAX request. You can do everything possible with this file, for example get the information from MySQL table. Additionally you can pass more attributes in the AJAX request, which you can get either from hidden input fields or just predefine them in the function.</p>
<p>You can also adjust this function to load contents from remote text files or any other files you can imagine. In the case of text files though, I would name the files using the name attribute of our drop down list, save it in the variable and pass it to the AJAX request as the file name. Have fun with this one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/jquery-drop-down-loading-content-according-to-option/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Setting boundaries for google map with more markers using jQuery</title>
		<link>http://www.designing4u.de/2008/05/setting-boundaries-for-google-map-with-more-markers-using-jquery/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=setting-boundaries-for-google-map-with-more-markers-using-jquery</link>
		<comments>http://www.designing4u.de/2008/05/setting-boundaries-for-google-map-with-more-markers-using-jquery/#comments</comments>
		<pubDate>Wed, 21 May 2008 16:13:24 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[automatic zoom level]]></category>
		<category><![CDATA[boundaries]]></category>
		<category><![CDATA[GLatLngBounds]]></category>
		<category><![CDATA[GMap2]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=17</guid>
		<description><![CDATA[For a project I'm currently working on I had to display more markers in one google map. No problem at all, but only if all of them are placed in one city. You can just set up default level for the city and display as many markers as you want. In my case I had [...]]]></description>
			<content:encoded><![CDATA[<p>For a project I'm currently working on I had to display more markers in one google map. No problem at all, but only if all of them are placed in one city. You can just set up default level for the city and display as many markers as you want. In my case I had to display latest markers, which were saved all over the country. One would say no problem at all, just use default zoom level for the country and display the markers. What if 10 latest markers were placed in one city though? It would look stupid if they would be displayed covering each other. Additionally we do not want to use an onload event, therefore, we will use jQuery in this example. For this kind of problem google came up with a pretty nice solution.<br />
<span id="more-17"></span><br />
if you want to see a working demo of this example, just click <a href="http://www.designing4u.de/examples/gmap/index.php" target="_blank">here</a>.</p>
<p>One of the classes available in second version of google maps is GLatLngBounds. Instantiating this class constructs a rectangle in geographical lengths. One of the methods, called 'extend' allows us to extend this rectangle to get a perfect zoom level. It means that every time our loop will display a new marker, we will call 'extend' method, which will additionally extend the boundaries of the rectangle, so it contains that given point. An additional functionality, which will be covered by this function is an event listener, which will display info window when a user clicks on one of the markers. At the end we will set the zoom level using one of the methods (getBoundsZoomLevel()) from another class, which can be called though, without instantiating it and use the getCenter() method from GLatLngBounds to get the center of our map. </p>
<p>To save us some time, I decided to skip the part where we are creating our XML document, which contains all necessary information (latitude, longitude and text for info window) to display our markers, therefore if you need that part to be covered as well, just let me know. Let's take a closer look at our code:<br />
[source:javascript]<br />
$(document).ready(function() {<br />
	//our map container and instance of GLatLngBounds<br />
	var mapContainer = $("#map")[0];<br />
	var bounds = new GLatLngBounds();</p>
<p>	//if browser is compatible<br />
	if (GBrowserIsCompatible()) {<br />
		//if div with given id was found<br />
		if(mapContainer) {<br />
			//instance of the GMap2 and some controls<br />
			var map = new GMap2(mapContainer);<br />
			map.addControl(new GLargeMapControl);<br />
			map.addControl(new GMapTypeControl);<br />
			map.addControl(new GScaleControl);<br />
			map.setCenter(new GLatLng(0,0),0);<br />
		}<br />
		//AJAX request<br />
		$.get('coordinates.xml',function(data) {<br />
			//we are looking for all marker and looping through them<br />
			$(data).find('marker').each(function(){<br />
				//serializing some data from the request<br />
				var lat    = $(this).attr('lat');<br />
				var lng    = $(this).attr('lng');<br />
				var desc   = $(this).attr('desc');<br />
				var point  = new GLatLng(lat,lng);<br />
				var marker = new GMarker(point);</p>
<p>				//new marker<br />
				map.addOverlay(marker);<br />
				//extending the boundaries<br />
				bounds.extend(point);<br />
				//and the info window<br />
				GEvent.addListener(marker, "click", function() {<br />
					marker.openInfoWindowHtml(desc);<br />
				});</p>
<p>			});<br />
			//setting the zoom level and the center<br />
			map.setZoom(map.getBoundsZoomLevel(bounds));<br />
			map.setCenter(bounds.getCenter());<br />
		});</p>
<p>	}</p>
<p>});<br />
[/source]<br />
I hope everything is clear. In case of any question just use the comment or contact form. One more thing left to show. Here is the content of xml file:<br />
[source:xml]<br />
<?xml version="1.0" encoding="iso-8859-1"?><br />
<markers><br />
	<marker lat="52.491783" lng="13.420009" desc="Pretty street" /><br />
	<marker lat="52.496375" lng="13.442661" desc="Good Food" /><br />
	<marker lat="52.514122" lng="13.448429" desc="Nice Bar" /><br />
	<marker lat="52.437259" lng="13.328170" desc="Stadion" /><br />
</markers><br />
[/source]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/setting-boundaries-for-google-map-with-more-markers-using-jquery/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>jQuery &#8211; show / hide button with AJAX functionality</title>
		<link>http://www.designing4u.de/2008/05/jquery-show-hide-button-with-ajax-functionality/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jquery-show-hide-button-with-ajax-functionality</link>
		<comments>http://www.designing4u.de/2008/05/jquery-show-hide-button-with-ajax-functionality/#comments</comments>
		<pubDate>Thu, 15 May 2008 13:11:40 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[AJAX request]]></category>
		<category><![CDATA[div]]></category>
		<category><![CDATA[hide]]></category>
		<category><![CDATA[show]]></category>
		<category><![CDATA[toggle]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=11</guid>
		<description><![CDATA[Recently I had to provide a really simple and easy solution for showing and hiding some information, which should be triggered on users' click. I didn't have to think for a long time, how I will solve this problem and the framework I choose was obviously jQuery. In this example I will show you, how [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had to provide a really simple and easy solution for showing and hiding some information, which should be triggered on users' click. I didn't have to think for a long time, how I will solve this problem and the framework I choose was obviously jQuery. In this example I will show you, how to trigger AJAX request and load some data into a div container and toggle between functions called every other click.<br />
<span id="more-11"></span><br />
Here is an example how our final solution will look like:<br />
<script src="http://code.jquery.com/jquery-latest.js"></script><br />
<script type="text/javascript">
	$(document).ready(function(){
		$('.link').toggle(
			function () {
				if($('#box').text() == '') {
					$.ajax({
						url:'http://www.designing4u.de/examples/jquery-show-hide/lorem.txt',
						method:'GET',
						success:function(r) {
							$('.link').html('hide');
							$('#box').html(r).hide().slideDown(1000);
						},
						error:function() {
							alert('file does not exist');
						}
					});
				} else {
					$(this).html('hide');
					$('#box').slideDown(1000);
				}
			},
			function () {
				$(this).html('show');
				$('#box').slideUp(1000);
			}
		);
	});
	</script></p>
<style type="text/css">
	#box {
		display:none;
		border:1px solid #000;
		width:500px;
	}
	.link {
		cursor:pointer;
		color:#ff0000;
		font-weight:bold;
	}
	</style>
<p><span class="link">show</span></p>
<div id="box"></div>
<p>Let's break it into pieces.<br />
[source:javascript]<br />
$(document).ready(function(){<br />
	$('.link').toggle(<br />
		function () {<br />
			if($('#box').text() == '') {<br />
				$.ajax({<br />
					url:'your_file.txt',<br />
					method:'GET',<br />
					success:function(r) {<br />
						$('.link').html('hide');<br />
						$('#box').html(r).hide().slideDown(1000);<br />
					},<br />
					error:function() {<br />
						alert('file does not exist');<br />
					}<br />
				});<br />
			} else {<br />
				$(this).html('hide');<br />
				$('#box').slideDown(1000);<br />
			}<br />
		},<br />
		function () {<br />
			$(this).html('show');<br />
			$('#box').slideUp(1000);<br />
		}<br />
	);<br />
});<br />
</script><br />
[/source]<br />
Because we want to write unobtrusive code, we want that our code should be executed whenever the DOM is ready. On each element with class 'link' we will toggle between two functions. In the first function we check, if our element with id 'box' is empty. If it is we use AJAX request to load the content from a remote file (in our case your_file.txt). For that reason you can use any other file. You can even pass parameters and request content relevant only for your case. For that reason you would have to add extra parameter 'data' in AJAX request of this function. </p>
<p>If the request was successful, we replace 'show' with 'hide', past the requested content in our container with id 'box', hide it and slide it down to get a nice visual effect. If this request retrieves an error, an alert box with information that the file wasn't found will be displayed. This function does the if - else check only because we want to do the AJAX request only once.</p>
<p>The second function is pretty simple. It basically replaces 'hide' with 'show' again and it slides up our div container. If a user clicks on 'show' button again, we wont need to use AJAX request anymore, because the text was already past in our DOM. Here is the HTML and CSS code for the body section of our document:<br />
[source:HTML]</p>
<style type="text/css">
#box {
	display:none;
	border:1px solid #000;
	width:250px;
}
.link {
	cursor:pointer;
	color:#ff0000;
	font-weight:bold;
}
</style>
<p><span class="link">show</span></p>
<div id="box"></div>
<p>[/source]<br />
If you want a nice effect of an arrow pointing up or down depending on the case, you can extend this function with some class replacements. Have fun with playing around with this one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/jquery-show-hide-button-with-ajax-functionality/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Handeling the dates and the leap year with AJAX and jQuery</title>
		<link>http://www.designing4u.de/2008/05/handeling-the-leap-year-with-ajax-and-jquery/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=handeling-the-leap-year-with-ajax-and-jquery</link>
		<comments>http://www.designing4u.de/2008/05/handeling-the-leap-year-with-ajax-and-jquery/#comments</comments>
		<pubDate>Tue, 13 May 2008 20:33:47 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[date validation]]></category>
		<category><![CDATA[drop down list]]></category>
		<category><![CDATA[leap year]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=6</guid>
		<description><![CDATA[Almost all web 2.0 platforms lean toward using an existing e-mail as a form of authorization and authentication. Usually you don't need to use a user name to login or register at a certain site. What they are mostly doing though is the birth date validation. I just encountered a site, which doesn't allow a [...]]]></description>
			<content:encoded><![CDATA[<p>Almost all web 2.0 platforms lean toward using an existing e-mail as a form of authorization and authentication. Usually you don't need to use a user name to login or register at a certain site. What they are mostly doing though is the birth date validation. I just encountered a site, which doesn't allow a registration to the people younger than 25 years old. If you went through the registration process and you are older than 25, your re-registration process might be blocked. If you really want to register though and have enough knowledge you can delete your cookies, restart your router (or any other method depending on how you are connected to internet) and register anyway, however probably 90 percent of users doesn't have that knowledge.<br />
<span id="more-6"></span><br />
There are plenty of tools around internet, which can display calendars with the help of JavaScript, however they are not completely clear enough for all people using internet. Therefore for one of my projects I had to provide three drop down fields, which should be able to handle birth date of new registered users. This task would be easy, if each month had the same amount of days. OK, even if every other month would have the same amount of days this task wouldn't be that difficult, but you cannot forget that February has 28 days and every four years we have a leap year, when February has 29 days. In this example I will show you, how I accomplished this task using the unobtrusive JavaScript, the beauty of AJAX and jQuery (and a little bit help of PHP:).</p>
<p>How to include jQuery libraries into your document you can read in this <a href="http://www.designing4u.de/2008/05/google-maps-and-jquery-drop-down-and-clickable-maps/" target="_blank" title="google maps and jquery - drop down and clickable maps">post</a>. After you've done that in the body section you need to include this three fields (year, month, day), which will handle user input.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;year&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Year<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;select</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;year&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;year&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;option</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/option<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;option</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;1959&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>1959<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/option<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	...
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;option</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;1986&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>1986<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/option<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/select<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;month&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Month<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;select</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;month&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;month&quot;</span> <span style="color: #000066;">disabled</span>=<span style="color: #ff0000;">&quot;disabled&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;option</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/option<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;option</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>January<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/option<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	...
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;option</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;12&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>December<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/option<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/select<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;day&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Day<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;select</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;day&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;day&quot;</span> <span style="color: #000066;">disabled</span>=<span style="color: #ff0000;">&quot;disabled&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;option</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/option<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/select<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>If you don't feel like copying the drop down for each year you can user "for" loop, which will do the work for you. In our case for year drop down we will have to paste this code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1959</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">1986</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;option value=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$i</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;<span style="color: #006699; font-weight: bold;">$i</span>&lt;/option&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We can actually do the same thing for months drop down, but in this case we will have to save all the months in an array and use "foreach" loop.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$month</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'January'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'February'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'March'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'April'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'May'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'June'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'July'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'August'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'September'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'October'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'November'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'December'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$month</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$months</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;option value=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$i</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;<span style="color: #006699; font-weight: bold;">$months</span>&lt;/option&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Easy, isn't it?:) Let's start to use some magic to make our example work. As you can notice I disabled month and day fields and will enable them first after we will get any input from user. Let's take a look at the code, which will enable those fields for us. Don't forget to put it in the head section of your document.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#year'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#month'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeAttr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'disabled'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This simple code will be executed after the DOM is loaded and will trigger the removeAttr function on our drop down with id 'month' on onchange event of id element 'year'. Let's move further and see what happens, if our user chooses a month.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#year'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#month'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeAttr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'disabled'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#month'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> leap_year <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#year'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> leap_days <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>leap_year<span style="color: #339933;">%</span>4 <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">29</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">28</span><span style="color: #339933;">;</span>
		$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
			data<span style="color: #339933;">:</span><span style="color: #3366CC;">'month='</span><span style="color: #339933;">+</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'&amp;leap='</span><span style="color: #339933;">+</span>leap_days<span style="color: #339933;">,</span>
			url<span style="color: #339933;">:</span><span style="color: #3366CC;">'leap_helper.php'</span><span style="color: #339933;">,</span>
			type<span style="color: #339933;">:</span><span style="color: #3366CC;">'POST'</span><span style="color: #339933;">,</span>
			error<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'something went wrong'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
			success<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>r<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#day'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeAttr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'disabled'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>r<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see we trigger a function on onchange event for id element 'month'. We read the value of the year drop down and save it in as leap variable. We then use modulo operator to check, if the division by four is equal zero. If it is, we assign 29 days to variable leap_days, in other case 28. We then call AJAX function of jQuery and prepare our data string, which we will use to pass the information about the month and the amount of days in leap year to our PHP script. To send the data we will use the POST method. If our script encounters error, we will alert a message, that something went wrong. (In this place you can do, whatever you feel like. For example it would be nice, if you would display a nice error message above the date input fields.) If we will get a success response from our script we will firs remove the disable attribute from the element with id 'day' and paste a drop down with the amount of days.</p>
<p>Let's take a closer look at our PHP file. You need to save it in the same directory and give it a name 'leap_helper.php'. If you want to use other names or directory structure, you need to change its' name in jQuery function.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$day_30</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$day_31</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$day_28</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$day</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'month'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$day_30</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">31</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$day</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;option value=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$i</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;<span style="color: #006699; font-weight: bold;">$i</span>&lt;/option&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'month'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$day_31</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">32</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$day</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;option value=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$i</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;<span style="color: #006699; font-weight: bold;">$i</span>&lt;/option&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'month'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$day_28</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'leap'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$day</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;option value=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$i</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;<span style="color: #006699; font-weight: bold;">$i</span>&lt;/option&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$day</span><span style="color: #339933;">;</span></pre></div></div>

<p>In our PHP file we save three arrays for months, with 30, 31 and 28 or 29 days and initialize our $day variable. After that we perform a simple check, if the month our user choose is in the array of months with different amount of days. If it is, then we create a string with exact amount of days for chosen month. In our last case we use additionally the leap variable to display amount of days in February according to the year chosen by the user.</p>
<p>I hope this short explanation will help you in developing your own application. Obviously to check, if 1644 was a leap year, it wont be enough to divide it by four. For this kind of case you need a little bit more complicated formula. For our case it's just enough, if you divide by four and pass this value to our PHP script, which will create a string for you. Have fun with this script, and as usually I'm waiting for your responses. Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/handeling-the-leap-year-with-ajax-and-jquery/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

