{"id":679,"date":"2012-01-27T10:56:55","date_gmt":"2012-01-27T10:56:55","guid":{"rendered":"http:\/\/www.osd.net\/blog\/?p=679"},"modified":"2013-02-08T09:13:02","modified_gmt":"2013-02-08T09:13:02","slug":"dynamic-jquery-datepicker-calendar","status":"publish","type":"post","link":"https:\/\/www.osd.net\/blog\/web-development\/javascript\/dynamic-jquery-datepicker-calendar\/","title":{"rendered":"Dynamic Jquery Datepicker calendar"},"content":{"rendered":"<p>Have you ever wanted to know what do you have to do tomorrow or in the next week without checking your agenda?<\/p>\n<p>Today I&#8217;m going to show you how to use <a title=\"JQuery Datepicker\" href=\"http:\/\/jqueryui.com\/demos\/datepicker\/\" target=\"_blank\">JQuery Datepicker<\/a> and <strong>AJAX<\/strong>\u00a0 to create a calendar that looks like this:<\/p>\n<p class=\"aligncenter\"><a href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2012\/01\/calendar.jpg\"><img class=\"size-medium wp-image-435 aligncenter\" title=\"calendar\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2012\/01\/calendar.jpg\" alt=\"jquery calendar\" height=\"350px\" \/><\/a><\/p>\n<p style=\"text-align: left;\">This dynamic datepicker calendar will allow users to click on a date and see the events. It&#8217;s easy to create and very useful. The best part is that only the dates from the calendar that contain events are the clickable ones. Only by watching you will quickly see in what days of the month you have events. Now let&#8217;s begin!<\/p>\n<p style=\"text-align: left;\">To create a<strong> jquery calendar<\/strong> we will use a<strong> HTML file<\/strong>, a <strong>Javascript file<\/strong> and a<strong> PHP file<\/strong> requested with<strong> AJAX<\/strong>. Let&#8217;s begin!<\/p>\n<p>1. The <strong>html <\/strong>file<\/p>\n<p>This is the file you&#8217;re going to see in the browser. This file is the most important one because it contains the link to the <strong>JQuery<\/strong> libraries and the <strong>stylesheets<\/strong>.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!-- stylesheets --&gt;\r\n\r\n&lt;!-- js files --&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.5\/jquery.min.js&quot;&gt;\/\/ &lt;![CDATA[\r\n    mce:0\r\n\/\/ ]]&gt;&lt;\/script&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;http:\/\/ajax.googleapis.com\/ajax\/libs\/jqueryui\/1.8\/jquery-ui.min.js&quot;&gt;\/\/ &lt;![CDATA[\r\n    mce:1\r\n\/\/ ]]&gt;&lt;\/script&gt;\r\n<\/pre>\n<p>Now that we&#8217;ve included the libraries, we have to add the output somewhere in the page. For this we&#8217;ll use 2 div elements (remember their ids because you&#8217;ll need them in the code). You don&#8217;t need to populate them. Just leave them like that.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!-- this div is used for the dialog box --&gt;&lt;\/pre&gt;\r\n&lt;div id=&quot;dialog&quot;&gt;&lt;\/div&gt;\r\n&lt;pre&gt;&lt;\/pre&gt;\r\n&lt;div id=&quot;datepicker&quot;&gt;&lt;\/div&gt;\r\n&lt;pre&gt;<\/pre>\n<p>2. The <strong>javascript<\/strong> file<\/p>\n<p>You can also put this code in the <strong>html<\/strong> file between the &#8220;&lt;script&gt;&#8221; tags. This <strong>script<\/strong> will have 2 parts:<\/p>\n<ul>\n<li>The first part contains the<strong> function<\/strong> that is invoked when the page is loaded:\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n$(document).ready(function()\r\n{\r\n\tvar selected_dates = new Array();\r\n\t\/\/ gets all the events from the database using AJAX\r\n\tselected_dates = getSelectedDates();\r\n\r\n\t$('#datepicker').datepicker({\r\n\t\tdateFormat: 'yy-m-d',\r\n\t\tinline: true,\r\n\t\tbeforeShowDay: function (date)\r\n\t\t{\r\n\t\t\t\/\/ gets the current month, day and year\r\n\t\t\t\/\/ Attention: the month counting starts from 0 that's why you'll need to +1 to get the month right\r\n\t\t\tvar mm = date.getMonth() + 1,\r\n\t\t\t\tdd = date.getDate(),\r\n\t\t\t\tyy = date.getFullYear();\r\n\t\t\tvar dt = yy + &quot;-&quot; + mm + &quot;-&quot; + dd;\r\n\r\n\t\t\tif(typeof selected_dates[dt] != 'undefined')\r\n\t\t\t{\r\n\t\t\t\t\/\/ puts a special class to the dates in which you have events, so that you could distinguish it from the other ones\r\n\t\t\t\t\/\/ the &quot;true&quot; parameter is used to know which are the clickable dates\r\n\t\t\t\treturn [true, &quot; my_class&quot;];\r\n\t\t\t}\r\n\r\n\t\t\treturn [false, &quot;&quot;];\r\n\t\t},\r\n\t\tonSelect: function(date)\r\n\t\t{\r\n\t\t\t\/\/ puts the event's title in the dialog box\r\n\t\t\t$(&quot;#dialog&quot;).attr(&quot;title&quot;,selected_dates[date]['event_title']); \/\/ for the first time you open the popup\r\n\t\t\t$(&quot;#dialog&quot;).dialog(&quot;option&quot;,&quot;title&quot;,selected_dates[date]['event_title']);\r\n\t\t\t\/\/ puts the event's description text in the dialog box\r\n\t\t\t$(&quot;#dialog&quot;).text(selected_dates[date]['event_description']);\r\n\t\t\t\/\/ show the dialog box\r\n\t\t\t$(&quot;#dialog&quot; ).dialog();\r\n        }\r\n\t});\r\n});\r\n<\/pre>\n<\/li>\n<li>The second part is the function that returns all the events from the database using AJAX:\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction getSelectedDates()\r\n{\r\n\tvar the_selected_dates = new Array();\r\n    $.ajax(\r\n    {\r\n        url: 'events.php',\r\n        dataType: 'json',\r\n        async: false,\r\n        success: function(data)\r\n        {\r\n\t\t\t$.each(data, function(n, val)\r\n            {\r\n                the_selected_dates[val.event_date] = val;\r\n            });\r\n        }\r\n    });\r\n    return the_selected_dates;\r\n}\r\n<\/pre>\n<\/li>\n<\/ul>\n<p>This is just an example of what you can obtain in the dialog box. You can also add a date (or anything else). There&#8217;s no limit.<\/p>\n<p>3. As you can see in the <strong>javascript file<\/strong>, we have a <strong>PHP<\/strong> document requested with <strong>AJAX<\/strong>. You&#8217;ll need a <strong>database<\/strong> that has the <strong>events<\/strong> which you want to show. This is the <strong>code<\/strong> for creating the table that we will use:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE TABLE IF NOT EXISTS `events` (\r\n  `event_id` int(11) NOT NULL auto_increment,\r\n  `event_date` date NOT NULL,\r\n  `event_title` varchar(100) NOT NULL,\r\n  `event_description` text NOT NULL,\r\n  PRIMARY KEY  (`event_id`)\r\n)\r\n<\/pre>\n<p>In <em>events.php<\/em>, take all the events and put them in array. Now encode it in a <strong>json<\/strong>.<br \/>\n<strong>NOTE: <\/strong>I recommend you to customize this function and take only the events from the month\/year where you have a large database.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$dates = array();\r\ntry\r\n{\r\n\t$stmt = $dbh-&gt;query('\r\n\t\t\t\t\t\t\tSELECT\r\n\t\t\t\t\t\t\t\t*\r\n\t\t\t\t\t\t\tFROM\r\n\t\t\t\t\t\t\t\tevents\r\n\t\t\t   \t\t\t');\r\n}\r\ncatch (PDOException $e)\r\n{\r\n\tprint($e-&gt;getMessage());\r\n\tdie;\r\n}\r\nwhile ($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC))\r\n{\r\n\t\/\/ because $row['event_date'] will have this form: 2012-01-10 and in Javascript we have 2012-1-10, we need to rewrite it in the way we use it in Javascript, so we could compare it\r\n\t$row['event_date'] =  date(&quot;Y-n-j&quot;, strtotime($row['event_date']));\r\n\t$dates[] = $row;\r\n}\r\necho json_encode($dates);\r\n<\/pre>\n<p>You can download an archive with the code <a title=\"dynamic jquery datepicker calendar\" href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2012\/06\/jquery-calendar.zip\">here<\/a>.<\/p>\n<p>If you have questions or suggestions please, don&#8217;t hesitate to tell me.<\/p>\n<div id=\"share-and-beer-container\">\t<div id=\"buy_me_a_beer_div\" class=\"single beer\">\n\t \t\n\t\t<div class=\"buy-beer\" onclick=\"document.getElementById('buy_me_a_beer_form').submit();\">\n\t\t\t<form action=\"https:\/\/www.paypal.com\/cgi-bin\/webscr\" id=\"buy_me_a_beer_form\" method=\"post\">\n\t\t\t\t<input type=\"hidden\" name=\"cmd\" value=\"_xclick\" \/>\n\t\t\t\t<input type=\"hidden\" name=\"business\" value=\"info@directaccess.ro\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_name\" value=\"A Beer For Dynamic Jquery Datepicker calendar\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_number\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"return\" value=\"https:\/\/www.osd.net\/blog\/web-development\/javascript\/dynamic-jquery-datepicker-calendar\/\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"amount\" value=\"5\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"undefined_quantity\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"currency_code\" value=\"USD\" \/>  \n\t\t\t<\/form>\n\t\t\t<p class=\"buy-beer-text\">If you liked this post <br \/> you can <strong>buy me a beer<\/strong><\/p>\n\t\t<\/div>\n\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Have you ever wanted to know what do you have to do tomorrow or in the next week without checking your agenda? Today I&#8217;m going to show you how to use JQuery Datepicker and AJAX\u00a0 to create a calendar that looks like this: This dynamic datepicker calendar will allow users to click on a date &hellip;<\/p>\n<div class=\"cta1\"><a href=\"https:\/\/www.osd.net\/blog\/web-development\/javascript\/dynamic-jquery-datepicker-calendar\/\">Read more<\/a><\/div>\n<div class=\"like-excerpt\"><div\n        class=\"fb-like\"\n        data-href=\"https:\/\/www.osd.net\/blog\/web-development\/javascript\/dynamic-jquery-datepicker-calendar\/\"\n        data-layout=\"button_count\"\n        data-action=\"like\"\n        data-show-faces=\"false\"\n        data-share=\"false\">\n        <\/div><\/div>","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[57,104,55,86,103,35],"_links":{"self":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/679"}],"collection":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/comments?post=679"}],"version-history":[{"count":29,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/679\/revisions"}],"predecessor-version":[{"id":744,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/679\/revisions\/744"}],"wp:attachment":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/media?parent=679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/categories?post=679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/tags?post=679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}