Wednesday, April 2, 2014

HTML5 and PHPMailer 101

-----
HTML5 and PHPMailer 101

INTRODUCTION

HTML5 is the latest standard for HTML. HTML5 was designed to replace both HTML 4, XHTML, and the HTML DOM Level 2. It was specially designed to deliver rich content without the need for additional plugins. All major browsers (Chrome, Firefox, Internet Explorer, Safari, Opera) support the new HTML5 elements and APIs, and continue to add new HTML5 features to their latest versions.

STEPS

0) Pre-requisite:
1) In Notepad++ Project, create Project201.
2) Create an html5 file, contactform101.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Contact Form</title>
<link type="text/css" rel="stylesheet" href="style.css">
</head>
<body>
    <header class="body">
        <h1>Contact Form</h1>
    </header>
    <section class="body">
    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
       
    <label>Name</label>
    <input name="name" placeholder="Type Here">
           
    <label>Email</label>
    <input name="email" type="email" placeholder="Type Here">
           
    <label>Message</label>
    <textarea name="message" placeholder="Type Here"></textarea>
        
    <input id="submit" name="submit" type="submit" value="Submit">
       
</form>
    </section>
    <footer class="body">
    </footer>
</body>
</html>
3) Browse contactform101.html.
4) create css file, style.css
@charset "utf-8";
/* CSS Document */
body {
        font-size:100%;
        font-family:Georgia, "Times New Roman", Times, serif;
        color:#3a3a3a;
}
.body {
        width:576px;
        margin:0 auto;
        display:block;
}
h1 {
        width:498px;
        height:64px;
        background:url(images/h1Bg.jpg);
        color:#fff;
        font-family:bebas;
        padding:17px 0px 0px 78px;
        letter-spacing:1px;
        font-size:2.2em;
        margin:0 auto;
}
form {
        width:459px;
        margin:0 auto;
}
label {
        display:block;
        margin-top:20px;
        letter-spacing:2px;
}
input, textarea {
        width:439px;
        height:27px;
        background:#efefef;
        border-radius:5px;
        -moz-border-radius:5px;
        -webkit-border-radius:5px;
        border:1px solid #dedede;
        padding:10px;
        margin-top:3px;
        font-size:0.9em;
        color:#3a3a3a;
}
        input:focus, textarea:focus {
                border:1px solid #97d6eb;
        }
textarea {
        height:213px;
        font-family:Arial, Helvetica, sans-serif;
        background:url(images/textareaBg.jpg);
        background-repeat:no-repeat;
background-size:cover;
}
#submit {
        background:url(images/submit.jpg);
        width:127px;
        height:38px;
        text-indent:-9999px;
        border:none;
        margin-top:20px;
        cursor:pointer;
}
        #submit:hover {
                opacity:0.9;
        }
footer a img {
        border:none;
        float:right;
        margin:0px 59px 40px 0px;
}
5) save the following images to ../images/ folder.
C:\Z\TRG-PHP-ADV\h1-bg.jpg
h1Bg.jpg
C:\Z\TRG-PHP-ADV\textarea-bg.jpg
textareaBg.jpg
C:\Z\TRG-PHP-ADV\submit.jpg
submit.jpg
6) Browse
7) Append simple php scripts to send the data (without validation and protection).
<?php
if ($_POST['submit']) {
    if (mail ($to, $subject, $body, $from)) {
        echo '<p>Your message has been sent!</p>';
    } else {
        echo '<p>Something went wrong, go back and try again!</p>';
    }
}
?>
Note: mail() function requires some configuration on php.ini. You may try phpmailer instead.
8) PHPMailer.

2) unzip the folder, rename as “phpmailer”, put it under your application.

3) create the following test file, mailer.php  (change your form action target to mailer.php)

<?php
require_once('class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

//$body             = file_get_contents('contents.html');
//$body             = eregi_replace("[\]",'',$body);
$body="test"; //$body=$_POST['message'];

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                          // 1 = errors and messages
                                          // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "tls";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 587;                   // set the SMTP port for the GMAIL server
$mail->Username   = "yourusername@gmail.com";  // GMAIL username
$mail->Password   = "yourpassword";            // GMAIL password

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";
//$mail->Subject    = $_POST['postTitle'];

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "recipient@gmail.com";
$mail->AddAddress($address, "John Doe");

//$mail->AddAttachment("images/phpmailer.gif");      // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
 echo "Mailer Error: " . $mail->ErrorInfo;
} else {
 echo "Message sent!";
}
?>    


AJAX and JQUERY 101

-----
AJAX and JQUERY 101

INTRODUCTION

jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code. jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls and DOM manipulation. The jQuery library contains the following features:
  • HTML/DOM manipulation
  • CSS manipulation
  • HTML event methods
  • Effects and animations
  • AJAX
  • Utilities
jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!

STEPS

0) Pre-requisite:
1) In Notepad++ Project, create Project201.
2) Create an html file, ajq101.html
2.1) Add the following codes to ajq101.html:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.get("demo_test.php",function(data,status){
    $("#div1").html(data + status);
    });
  });
});
</script>
</head>
<body>
<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button>Get External Content</button>
</body>
</html>
2.2) Browse the file http://localhost/php201/ajq101.html and click the button.
- The script is requesting a resource demo_test.txt from the server but the server couldn’t find it.
3) Add AJAX resource file.
3.1) Create a file demo_test.txt . Add the following codes:
<h2>jQuery and AJAX is FUN!</h2>
<p id=’p1’>This is some text in a paragraph.</p>
3.2) Reload  http://localhost/php201/ajq101.html and click the button.
Observe that:
a) A new text is added when the button is clicked.
b)  jquery.min.js is loaded from ajax.googleapis.com
4) Change resource file to PHP script.
4.1) create demo_test.php. Add the following codes:
<?php
echo "<h2>jQuery and AJAX is FUN!</h2>";
echo "<p id='p1'>This is some text in a paragraph.</p>";
?>
5.2) Edit ajq101.html. Change demo_test.txt to demo_test.php:

AJAX 101

-----
AJAX 101

INTRODUCTION

What is AJAX?
AJAX = Asynchronous JavaScript and XML. AJAX is a technique for creating fast and dynamic web pages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page. Classic web pages, (which do not use AJAX) must reload the entire page if the content should change. Examples of applications using AJAX: Google Maps, Gmail, Youtube, and Facebook tabs.
AJAX was made popular in 2005 by Google, with Google Suggest. Google Suggest is using AJAX to create a very dynamic web interface: When you start typing in Google's search box, a JavaScript sends the letters off to a server and the server returns a list of suggestions.

STEPS

0) Pre-requisite:
1) In Notepad++ Project, create Project201.
2) Create an html file, ajax101.html
2.1) Add the following codes to ajax101.html:
<!DOCTYPE html>
<html>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>

2.1.1) Take note that the Line No. says <!DOCTYPE html> which means this is HTML 5 document.
2.2) Browse the file.
- At the moment, clicking the Change Content button will not give any effect. Refer Line No. 6 in Step 2.1, function loadXMLDoc() doesn’t exist.
3) Adding function loadXMLDoc()
3.1) Insert the <head> element to ajax101.html by adding the following codes in between the tag <html> and <body>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
- Take note that since this is HTML 5 document (refer Step 2.1.1) by default <script> refers to JavaScript.
3.2) Run AJAX.
3.2.1) Activate Chrome Developer Tools.
3.2.3) Click Change Content.
3.2.4) Observe that:
a) Chrome is requesting the file ajax_info.txt from the directory /php201.
b) GET request is used.
c) Status is 404 Not Found
4) Add AJAX resource file.
4.1) Create a file ajax_info.txt . Add the following codes:
<p>AJAX is not a new programming language.</p>
<p>AJAX is a technique for creating fast and dynamic web pages.</p>
4.3) This time, the server returns Status 200 (OK) and you see the output as above.
5) Change resource file to PHP script.
5.1) create ajax_info.php. Add the following codes:
<?php
echo "<p>AJAX is not a new programming language.</p>";
echo "<p>AJAX is a technique for creating fast and dynamic web pages.</p>";
?>
5.2) Edit ajax101.html. Change ajax_info.txt to ajax_info.php:

SUMMARY

With AJAX, the JavaScript does not have to wait for the server response, but can instead:
- execute other scripts while waiting for server response
- deal with the response when the response ready
Important keywords used:
- XMLHttpRequest()
- xmlhttp.onreadystatechange
- xmlhttp.readyState
- xmlhttp.responseText
- xmlhttp.open
- xmlhttp.send

FURTHER EXERCISE

Labels