<?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>Developing Ideas</title>
	<atom:link href="http://blog.arthurgouveia.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.arthurgouveia.com</link>
	<description>&#34;De nihilo nihilum&#34;</description>
	<lastBuildDate>Mon, 31 Aug 2009 15:03:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Simple form submit using Ajax and jQuery</title>
		<link>http://blog.arthurgouveia.com/tutorials/simple-form-submit-using-ajax-and-jquery/</link>
		<comments>http://blog.arthurgouveia.com/tutorials/simple-form-submit-using-ajax-and-jquery/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 17:59:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[tutorials]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.arthurgouveia.com/?p=10</guid>
		<description><![CDATA[Hi everyone!
It&#8217;s been a long since my last post, and I hope I can start posting more often.
Today I&#8217;m bringing you a small tutorial of how you can do a simple form that&#8217;s uses ajax and jQuery to do the job you want.
We&#8217;ll not be focusing on best practices or teaching any CSS. I&#8217;ve upload [...]]]></description>
			<content:encoded><![CDATA[<p>Hi everyone!</p>
<p>It&#8217;s been a long since my last post, and I hope I can start posting more often.<br />
Today I&#8217;m bringing you a small tutorial of how you can do a simple form that&#8217;s uses ajax and jQuery to do the job you want.</p>
<p>We&#8217;ll not be focusing on best practices or teaching any CSS. I&#8217;ve upload a live example where you can see it in action and also get the source code. If you&#8217;re interested, just click <a href="http://arthurgouveia.com/itworks/jQueryAjaxForm/" target="_blank">here</a>. Feel free to comment or e-mail about doubts or suggestions. They are all going to be welcome and answered ASAP.</p>
<p><span id="more-10"></span></p>
<p>So, the first thing we&#8217;ll be doing is to link the jQuery file, that you can download <a href="http://jquery.com/" target="_blank">here</a>. After the download, put it inside your project&#8217;s directory.</p>
<pre class="brush:js">
<script src="js/jquery-1.3.2.min.js" type="text/javascript">
</script>
</pre>
<p>In my case, I&#8217;ve created a very suggestive directory do store my JavaScript files called &#8220;js&#8221;, one called &#8220;css&#8221; to store (guess!) my CSS files and another one called &#8220;imgs&#8221; to store (hard, huh?) my images.</p>
<p>Now we&#8217;re going to create a simple form structure. I&#8217;ve built it in order to submit the name, e-mail and a message of the user. All these fields will be required, and this validation will be made on the server side. Feel free to add some JavaScript to validate it on the client side too (which is a very nice idea).</p>
<pre class="brush:xhtml">
<div id="form">
<table>
<tr>
<td><label for="name">Name:</label></td>
<td>
<input type="text" id="name" name="name" size="29"/></td>
</tr>
<tr>
<td><label for="email">E-mail:</label></td>
<td>
<input type="text" id="email" name="email" size="29"/></td>
</tr>
<tr>
<td><label for="msg">Message:</label></td>
<td><textarea id="msg" name="msg" cols="28" ></textarea></td>
</tr>
<tr>
<td>
<input type="button" value="Send" id="submitButton" class="submitButton"/></td>
</tr>
<tr id="status">
<td id="statusImage"><img src="imgs/ajax-loader.gif" alt="Loading" /></td>
<td id="statusMessage">Sending request...</td>
</tr>
</table></div>
</pre>
<p>The last row, as you can see, already has the loading image and also the message. But we don&#8217;t want to show it to the user yet. In order to do it, be sure to set the CSS for the TR &#8220;status&#8221; as &#8220;display: none&#8221;.</p>
<p>Now let&#8217;s go to the jQuery part. I&#8217;m going to assume that you already have some experience using it.</p>
<p>If you don&#8217;t have, my suggestion is for you to visit and watch all the videos of the &#8220;jQuery for Absolute Beginners&#8221;, from In The Woods Blog, by Jeffrey Way. It really helped me out, and that&#8217;s why I strongly recommend you to go <a href="http://blog.themeforest.net/screencasts/jquery-for-absolute-beginners-video-series/" target="_blank">there</a>.</p>
<p>So, basically what we are going to do is:</p>
<p>1 &#8211; Capture the click on the button;<br />
2 &#8211; Show an image and a text to say that the request is being sent;<br />
3 &#8211; Disable the submit button, so that the user won&#8217;t click again until is finished;<br />
4 &#8211; Get all the data provided and send to the PHP file (or whatever we want);<br />
5 &#8211; Once all the actions are done, the PHP file returns something, we capture it and show the return message;<br />
6 &#8211; Enable the submit button again.</p>
<p>For steps 1, 2 and 3 we&#8217;ll have:</p>
<pre class="brush:js">
  $(function(){

    	$('#submitButton').click(function(){
        	if($('#status').css('display') == 'none')
        	   $('#status').toggle();

    		$('#statusImage img').attr('src', 'imgs/ajax-loader.gif');
                $('#statusImage img').attr('alt', 'Loading');
                $('#statusMessage').html('Sending request...');

    	        $('#submitButton').attr('disabled', 'disabled');
// to be continued...
</pre>
<p>This will make the &#8220;#status&#8221; show up, then will change the image&#8217;s &#8220;src&#8221; and &#8220;alt&#8221; attributes to loading (ok, it was already like this, but after the first time you click it won&#8217;t be).</p>
<p>It will fill the row with some info, telling the user that the request is being sent. After that let&#8217;s get all the data provided and send to the PHP using the jQuery&#8217;s ajax way. I think that the code itself is very simple, but if you have doubts or if you want to know more about the other ajax options, just visit the <a href="http://docs.jquery.com/Ajax/jQuery.ajax" target="_blank">method documentation</a>.</p>
<pre class="brush:js">
//continuation
// steps 4 and 5
var name = $("#name").val();
var email = $("#email").val();
var msg = $("#msg").val();

$.ajax({
url: 'doSomething.php',
type: 'POST',
data: 'name=' + name + '&amp;email=' + email + '&amp;msg=' + msg,

// to be continued ...
</pre>
<p>Now that we have set the ajax options, we have to capture the response provided by the &#8220;doSomething.php&#8221; and show to the user.</p>
<pre class="brush:js">
// continuation

success: function(response){
    	    	   if(response == 'success') {
    	    		   $('#statusImage img').attr('src', 'imgs/success.png');
    	    		   $('#statusImage img').attr('alt', 'Success!');
    	    		   $('#statusMessage').html('The message was sent successfully!');
    	    	   } else {
    	    		   $('#statusImage img').attr('src', 'imgs/error.png');
                           $('#statusImage img').attr('alt', 'Fail!');
                           $('#statusMessage').html(response);
    	    	   }

    	           // step 6
    	           $('#submitButton').removeAttr('disabled');

    	    	}
    	    });
    	});
    });
</pre>
<p>Now we are going to make the PHP file &#8220;doSomething&#8221;. As I said before, it&#8217;s just a simple PHP file to represent the action you want to do. It simply checks if all the fields were filled, return a success message if so. Else, it returns a message that would be your error.</p>
<pre class="brush:php">/*
 * This file does nothing, basically.
 * Here you specify whatever you want to do with your data.
 */

if($_POST){
	if( $_POST['name'] != '' &amp;&amp; $_POST['email'] != '' &amp;&amp; $_POST['msg'] != ''){
		echo 'success';
		return;
	} else {
        echo 'Name, E-mail and Message fields must be filled.';
        return;
	}
}
echo 'What do you want to $_GET here?';
</pre>
<p>That&#8217;s pretty much it.<br />
If all the fields were filled or not, it returns a message and that will cause jQuery to fill the last table row with a nice image and a message so that the user knows what happened <img src='http://blog.arthurgouveia.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Thank you for reading!<br />
I&#8217;ll be waiting for your comments and doubts!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.arthurgouveia.com/tutorials/simple-form-submit-using-ajax-and-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: 12 tips to optimize your code!</title>
		<link>http://blog.arthurgouveia.com/tips/php-12-tips-to-optimize-your-code/</link>
		<comments>http://blog.arthurgouveia.com/tips/php-12-tips-to-optimize-your-code/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 10:59:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[tips]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Code Improvement]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.arthurgouveia.com/?p=5</guid>
		<description><![CDATA[
If a method can be static, declare it static. Speed improvement is by a factor of 4.
Avoid magic like __get, __set, __autoload
require_once() is expensive
Use full paths in includes and requires, less time spent on resolving the OS paths.
If you need to find out the time when the script started executing, $_SERVER['REQUEST_TIME'] is preferred to time()
See [...]]]></description>
			<content:encoded><![CDATA[<ol>
<li>If a method can be static, declare it static. Speed improvement is by a factor of 4.</li>
<li>Avoid magic like __get, __set, __autoload</li>
<li><strong>require_once()</strong> is expensive</li>
<li>Use full paths in includes and requires, less time spent on resolving the OS paths.</li>
<li>If you need to find out the time when the script started executing, $_SERVER['REQUEST_TIME'] is preferred to time()</li>
<li>See if you can use strncasecmp, strpbrk and stripos instead of regex</li>
<li>str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4</li>
<li>If the function, such as string replacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement statements, passing one character at a time, instead of one line of code that accepts arrays as search and replace arguments.</li>
<li>Error suppression with @ is very slow.</li>
<li>$row['id'] is 7 times faster than $row[id]</li>
<li>Error messages are expensive</li>
<li>Do not use functions inside of for loop, such as for ($x=0; $x &lt; count($array); $x) The count() function gets called <strong>each time.</strong></li>
</ol>
<p><b>From: <a href="http://www.moskalyuk.com/blog/php-optimization-tips/1272">moskalyuk</a></b>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.arthurgouveia.com/tips/php-12-tips-to-optimize-your-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello world!</title>
		<link>http://blog.arthurgouveia.com/none/hello-world/</link>
		<comments>http://blog.arthurgouveia.com/none/hello-world/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 22:39:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[none]]></category>

		<guid isPermaLink="false">http://blog.arthurgouveia.com/?p=1</guid>
		<description><![CDATA[Welcome everyone!
Well, this is my first post on the blog, so I don&#8217;t have much to say&#8230; yet.
Soon I&#8217;ll start posting some solutions on designing (XHTML, CSS, &#8230;) and also on programming languages (such as PHP, Java, Python, JS). Also, you can spect me to post some nice texts I may find on the web [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome everyone!</p>
<p>Well, this is my first post on the blog, so I don&#8217;t have much to say&#8230; yet.</p>
<p>Soon I&#8217;ll start posting some solutions on designing (XHTML, CSS, &#8230;) and also on programming languages (such as PHP, Java, Python, JS). Also, you can spect me to post some nice texts I may find on the web that talk about IT or any other things that I find interesting to share with you all.</p>
<p>That&#8217;s all for now.</p>
<p>&#8220;Goodbye, and thanks for all the fish&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.arthurgouveia.com/none/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
