jQuery UI Quick Start

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.9.1.js"></script>
    <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>
<body>
<button>A button element</button>
</body>
</html>

Linux Grub Change Boot Order

> sudo vi /etc/default/grub

- Change GRUB_DEFAULT=4
(4 is usually Windows boot)

> sudo update-grub

PHP Show all Error

Add these lines to the beginning of the php script:
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );

PHP Send Email via Google SMTP Server

To use the following script, you need:
1. Install PEAR Mail (See: Install PHP PEAR)
2. Add php_openssl.dll extension in php.ini
3. A google account

Function:
require_once( 'Mail.php' );

// Before use:
// 1. Add a php extension in php.ini: php_openssl.dll
// 2. Set $smtpUsername and $smtpPassword (Use a google account)
function sendmail( $from, $to, $subject, $body ) {
    $smtpHost = 'ssl://smtp.gmail.com';
    $smtpPort = '465';
    $smtpUsername = 'example@gmail.com';
    $smtpPassword = 'password';
    $headers = array(
        'From' => $from,
        'To' => $to,
        'Subject' => $subject
    );
    $smtp = @Mail::factory('smtp', array(
            'host' => $smtpHost,
            'port' => $smtpPort,
            'auth' => true,
            'username' => $smtpUsername,
            'password' => $smtpPassword
        ));
    $mail = @$smtp->send($to, $headers, $body);

    if ( @PEAR::isError($mail) ) {
        return false;
    } else {
        return true;
    }
}

Usage:
$from = 'from@gmail.com';
$to = 'to@gmail.com';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";
var_dump( sendmail( $from, $to, $subject, $body ) );

Install PHP PEAR

1. Download go-pear.phar

2. Go to the directory you want to install pear (It should NOT be inside www directory, I will install in \path\to\php\pear)

3. Copy and run go-pear.phar at the pear path:
> php go-pear.phar

4. Press Enter (to select system)

5. Update: 11. Name of configuration file (Where to create the pear.ini)

6. Update: 12. Path to CLI php.exe (Tell the pear where is php.exe)

7. Press Enter to start installation

8. Edit php.ini, add/edit the include_path:
include_path=".;\to\pear\path"
9. Restart PHP service

10. Done.

Bonus:
To install a package:
- Go to pear path, run:
> pear install package_name

jQuery Validation Plugin Template

jQuery Validation Plugin makes html form validation easier. You need to setup jQuery and jQuery Validation Plugin to make it works.

Header:
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>

Script:
<script>
$(function() {
    $( "#formId" ).validate({
        errorClass: 'alertText smallText',
        rules: {
            email: {
                required: true,
                email: true
            },
            username: {
                required: true
            },
            password: {
                required: true
            }
        },
        messages: {
            email: {
                required: 'Please input email',
                email: 'The email format is invalid'
            },
            username: 'Please input username',
            password: 'Please input password'
        }
    });
});
</script>

Form:
<form id="fromId" action="" method="post">
    <fieldset>
        <p>
            <input type="text" name="email" placeholder="email" value="" />
        </p>
        <p>
            <input type="text" name="username" placeholder="username" value="" />
        </p>
        <p>
            <input type="text" name="password" placeholder="password" value="" />
        </p>
        <p>
            <input type="submit" name="submit" value="Go" />
        </p>
    </fieldset>
</form>

PHP Validate Email

Note: PHP version >= 5.2.0
if ( filter_var( $_POST[ 'email' ] , FILTER_VALIDATE_EMAIL ) ) {
    echo 'Email ok';
}