<?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>Bits and Pieces of This and That</title>
	<atom:link href="http://randomcoding.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://randomcoding.com</link>
	<description>a random coding blog</description>
	<lastBuildDate>Sat, 24 Dec 2011 16:39:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Debugging Tips for Beginners</title>
		<link>http://randomcoding.com/2011/12/debugging-tips-for-beginners/</link>
		<comments>http://randomcoding.com/2011/12/debugging-tips-for-beginners/#comments</comments>
		<pubDate>Sat, 24 Dec 2011 16:39:17 +0000</pubDate>
		<dc:creator>John Pereira</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://randomcoding.com/?p=23</guid>
		<description><![CDATA[A common type of question I find on Stack Overflow are beginners having trouble with figuring out why their AJAX application / functionality isn&#8217;t working. They usually go something like this&#8230; I have two input boxes which accept the username and &#8230; <a href="http://randomcoding.com/2011/12/debugging-tips-for-beginners/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A common type of question I find on <a href="http://stackoverflow.com/">Stack Overflow </a>are beginners having trouble with figuring out why their AJAX application / functionality isn&#8217;t working. They usually go something like this&#8230;</p>
<blockquote><p>I have two input boxes which accept the username and password and sends it to the PHP login page via AJAX. But when I type in my credentials, I don&#8217;t get logged in. Why isn&#8217;t it working?</p></blockquote>
<p>When starting out to fix a problem, you need to first find out what the problem is. To do that you need to isolate where the problem lies, so break it down into its individual components. In no particular order, these would be</p>
<ul>
<li>The Javascript and AJAX</li>
<li>The PHP and server side code</li>
<li>The DOM and HTML</li>
</ul>
<div>Let&#8217;s look at each of them in a bit more detail</div>
<div></div>
<h2>The Javascript</h2>
<div>I do my development on <a href="http://www.mozilla.org/en-US/firefox/new/">Firefox</a> because I&#8217;ve found it has the best addons for the job. If you haven&#8217;t already, install <a href="http://getfirebug.com/">Firebug</a> for Firefox. In the page that has the AJAX functionality, open the Firebug console by hitting F12. Make sure the console is enabled (a reload of the page may be required) and carry out the action that triggers the AJAX response. If your JS code works, you should see something like the following in the console.</div>
<div>
<div id="attachment_24" class="wp-caption aligncenter" style="width: 310px"><a href="http://randomcoding.com/wp-content/uploads/2011/12/console.png"><img class="size-medium wp-image-24" title="Firebug Console" src="http://randomcoding.com/wp-content/uploads/2011/12/console-300x211.png" alt="Firebug console in action" width="300" height="211" /></a><p class="wp-caption-text">Firebug console in action</p></div>
</div>
<div>If you see any error messages or &#8216;not founds&#8217; for any of your requests, then you have a problem. Clicking on the request allows you to confirm that the correct request and response is happening in your application. If the requests and responses are correct, then your problem lies in either the HTML or your JS callback.</div>
<div></div>
<div>If you&#8217;re getting 404 messages for the request, that mean the PHP file you&#8217;re pointing to doesn&#8217;t exist. Update your code to fix the problem. Do the same if you&#8217;re seeing JS error messages. Make good use of the console (which can run live JS that affects your page) and the debugger (where you can walk through your JS files) .</div>
<div></div>
<div>If on the other hand, your request is sent properly but the response is incorrect then you need to look at your PHP page.</div>
<h2></h2>
<h2>The PHP and Server Side Code</h2>
<p>When you work with AJAX, it helps if you make sure that your application works without AJAX initially. This serves the double purpose of making sure your core functionality works and having a fallback when Javascript is disabled. Assuming you&#8217;re working with GET and not POST, either copy the request URL from Firebug or craft your own and access it in the browser. If you get an error then you&#8217;ve isolated (one of) your problem(s)! Find out what&#8217;s wrong and fix it and then see if the AJAX functionality works.</p>
<p>Using an IDE when coding gives you many benefits. Although you may think that notepad might be enough for your needs, something like <a href="http://www.eclipse.org/projects/project.php?id=tools.pdt">Eclipse </a>(which I use) or <a href="http://netbeans.org/">NetBeans </a>(which I&#8217;ve used) can come in very handy to catch some problems as you code them.</p>
<p>And don&#8217;t forget to have a debugger setup. I mainly use <a href="http://xdebug.org/">Xdebug</a>, but you can just as easily use <a href="http://www.zend.com/en/community/pdt">ZendDebug</a>.</p>
<p>Once you make sure you have the JS async call working properly with the serverside PHP script, you can look at the callback and the DOM.</p>
<h2>The DOM and HTML</h2>
<p>The most common reason for AJAX updates not working on the client side is usually because the element you are trying to update doesn&#8217;t exist. Confirm that the element exists first. I usually use the .length property or the <a href="http://api.jquery.com/toggle/">toggle()</a> method to make sure I have the correct reference.</p>
<p>And for any event issues, make sure that the DOM element exists before you bind any events to it. A related cause is when you bind an event with an element but then update the  element in thee DOM so you lose the reference. An easy solution to avoid this problem is to use something like the <a href="http://api.jquery.com/delegate/">.delegate()</a> method if you&#8217;re using jQuery.</p>
<p>To sum it up, whenever you run into a problem carry out the following steps.</p>
<p>1. Isolate the problem</p>
<p>2. Focus on the problem area</p>
<p>3. Fix the bug (easy part!)</p>
]]></content:encoded>
			<wfw:commentRss>http://randomcoding.com/2011/12/debugging-tips-for-beginners/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Thing About Debugging</title>
		<link>http://randomcoding.com/2011/11/the-thing-about-debugging/</link>
		<comments>http://randomcoding.com/2011/11/the-thing-about-debugging/#comments</comments>
		<pubDate>Sun, 27 Nov 2011 10:43:31 +0000</pubDate>
		<dc:creator>John Pereira</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://randomcoding.com/?p=20</guid>
		<description><![CDATA[Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. - Brian Kernighan]]></description>
			<content:encoded><![CDATA[<blockquote><p>Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.</p>
<p>- Brian Kernighan</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://randomcoding.com/2011/11/the-thing-about-debugging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Blog is Born. Again.</title>
		<link>http://randomcoding.com/2011/11/a-blog-is-born-again/</link>
		<comments>http://randomcoding.com/2011/11/a-blog-is-born-again/#comments</comments>
		<pubDate>Sun, 27 Nov 2011 09:43:39 +0000</pubDate>
		<dc:creator>John Pereira</dc:creator>
				<category><![CDATA[Career]]></category>
		<category><![CDATA[introduction]]></category>
		<category><![CDATA[personal]]></category>

		<guid isPermaLink="false">http://randomcoding.com/?p=5</guid>
		<description><![CDATA[I used to have a work / technical blog. But then I got bored of it and took it down after a while. It&#8217;s been a couple of years since that happened and now I find myself constantly wanting to &#8230; <a href="http://randomcoding.com/2011/11/a-blog-is-born-again/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I used to have a work / technical blog. But then I got bored of it and took it down after a while. It&#8217;s been a couple of years since that happened and now I find myself constantly wanting to write down the things that are going through my mind. Mostly for my own reference (what better way to learn than to teach something) but also to showcase the soft skills that are much needed in the field I work in. Let&#8217;s jump right in with an introduction.</p>
<p>I&#8217;m John Pereira and I&#8217;m a software developer. My chosen field is the Web and I mostly work with PHP, Javascript and NodeJS. I&#8217;m also familiar with Java to a certain degree. I&#8217;m in the process of <em>deciding</em> to learn a new language but it&#8217;s currently an even split between Python, Ruby and the Android SDK (Java). Hoping to add the final candidate into the 2012 new year resolutions.</p>
<p>I&#8217;ve been coding since I was in school, and I&#8217;ve been doing it professionally for about 4 years now. I started out with QBasic and then moved on to VB5 and then a bunch of different languages when I was studying for my bachelors. Most of my work has revolved around large business process oriented systems.</p>
<p>I code for fun and for profit but I haven&#8217;t yet contributed to an open source project. This is something I hope to rectify soon, suggestions are welcome. My current toolbox is made up of Zend Framework, CakePHP, WordPress, a bunch of NodeJS libs, MongoDB and jQuery so it would be nice to extend my knowledge in those areas. The only community I have any level of activity is on StackOverflow.</p>
<p><a href="http://stackexchange.com/users/ab18e6c6d2f648fbb6e18dede6490490"><img title="profile for JohnP on Stack Exchange, a network of free, community-driven Q&amp;A sites" src="http://stackexchange.com/users/flair/ab18e6c6d2f648fbb6e18dede6490490.png" alt="profile for JohnP on Stack Exchange, a network of free, community-driven Q&amp;A sites" width="208" height="58" /><br />
</a></p>
<p>It&#8217;s not all fun and games though, as a <a href="http://steamcommunity.com/id/jomanlk/games">dutiful gamer</a> I put in whatever time I can grinding through levels (where applicable). I&#8217;ve already planned out my December &#8211; January playlist like so &#8211; Battlefield 3, Modern Warfare 3, Arkham City and Skyrim. <a href="http://www.goodreads.com/review/list/6050802-john-pereira">I also read a lot of fantasy, sci fi and Terry Pratchett</a> (Yes he rates his own mention because he&#8217;s just that good).</p>
<p>And in case I didn&#8217;t make this clear earlier, I <em>really</em> enjoy what I do.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://randomcoding.com/2011/11/a-blog-is-born-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

