Tuesday, May 28, 2013

Popular SMTP settings

Following are setting information to configure a connection to the particular SMTP servers. SMTP or Simple Mail Transfer Protocol is relate with the mail transferring over the Internet.

Gmail
SMTP server address : smtp.gmail.com
SMTP user name : Your full Gmail address (e.g. example@gmail.com)
SMTP password : Your Gmail password
SMTP port : 465
SMTP TLS/SSL required : Yes
Ymail
SMTP server address : smtp.mail.yahoo.com
SMTP user name : Your full Yahoo! Mail email address (including "@yahoo.com")
SMTP password : Yahoo! Mail password
SMTP port : 465
SMTP TLS/SSL required : Yes
AOL
SMTP server address : smtp.aol.com
SMTP user name : Your AOL Mail screen name (what comes before "@aol.com" in your AOL Mail address; if your address is me@aol.com, for example, the corresponding screen name is "me")
SMTP password : Your AOL Mail password
SMTP port : 587 (alternative: 465)
SMTP TLS/SSL required : Yes
Hotmail
SMTP server address : smtp.live.com
SMTP user name : Your full Windows email address (including "@yahoo.com")
SMTP password : Windows live hotmail password
SMTP port : 587
SMTP TLS/SSL required : Yes

Assume that we have sent a E-Mail to the address of abc@gmail.com through GMAIL mail engine. Since we have sent it through GMAIL, our mail is sent to the GMAIL SMTP server via the port 465. Then it reads the recipient's address. The part after the @ says recipient also holds an gmail account. So, SMTP server redirect the mail to GMAIL POP3 server. POP3 server identifies the particular recipient from the address part before the @ and link the particular mail to his account. Recipient log into the POP3 server using his user name and password and read the mail.

Assume what happens if we have sent the particular mail to abc@gmail.com via the YMAIL mail engine. If so, YMAIL SMTP server look at the address part after the @ and understand that recipient doesn't has an account in YMAIL POP3 server. So, it re-directs the mail to the particular SMTP server indicated at the address. After it has been sent to the GMAIL SMTP server same routine is happening as mentioned in the above paragraph.

Sunday, May 26, 2013

Find number of days between given two days in PHP

We are going to study how can we calculate the number of days between given two days. For the example I am going to find the number of days between current date and another given date.

<?php

    $year="1990";
    $month="March";
    $day="23";


$today=date('Y-m-d'); // in-built function to get current date.

$datetime1 = date_create($year.'-'.$month.'-'.$day); //Given date
$datetime2 = date_create($today); //System date for current date
$interval = date_diff($datetime1, $datetime2); //$datetime2 - $datetime1
$date_amount= $interval->format('%R%a');

echo($date_amount);

?>

Automatically populate SELECT items in HTML using JAVASCRIPT

We are going to learn how we can automatically populate a data set to view in a select element in HTML using javascript. As an example we will populate set of years, months and dates inside of select element which can be used to input a particular birthday in a HTML form.


<script language="javascript">

             function loadYears(){
            var sel = document.getElementById('year');

// 'new Date().getFullYear()' is used to get current year.

            for(var i = 1950; i <= new Date().getFullYear(); i++) {
            var opt = document.createElement('option');
            opt.innerHTML = i;
            opt.value = i;
            sel.appendChild(opt);
            }
            }
           
            function loadDays(){


            document.getElementById('day').innerHTML = ""; //initially clears the data items which were included by the previous function invokes.

            var sel = document.getElementById('day');

     // if/else set is to decide the number of days for a particular month.     

 
   if(document.getElementById('month').value!='February' && document.getElementById('month').value!='April' && document.getElementById('month').value!='June' && document.getElementById('month').value!='August' && document.getElementById('month').value!='November' && document.getElementById('month').value!='September'){
                for(var i = 1; i < 32; i++) {
                var opt = document.createElement('option');
                opt.innerHTML = i;
                opt.value = i;
                sel.appendChild(opt);
                }
                }else{
               
                for(var i = 1; i < 31; i++) {
                var opt = document.createElement('option');
                opt.innerHTML = i;
                opt.value = i;
                sel.appendChild(opt);
                }
            }
            }
           
            function loadMonths(){

//JavaScript array to store the names of the months
 var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];    
            var sel = document.getElementById('month');
            for(var i = 0; i < months.length; i++) {
            var opt = document.createElement('option');
            opt.innerHTML = months[i];
            opt.value = months[i];
            sel.appendChild(opt);
            }
            }
        </script>

<body onload="loadYears();loadMonths()"> <!-- According to my logic Year and month generation functions should be invoked at the body onload event -->

<!-- Following are the particular select elements on the html page -->
<select id="year" name="year"></select>
               
<select id="month" name="month" onclick="loadDays()"> </select><!-- According to my logic days should be loaded at the time of choosing the month. So I invoked that function by the event of onclick here. -->
                           
<select id="day" name="day"></select>

Friday, May 17, 2013

Example Multi Dimensional Array In PHP

Hi ! Multi Dimensional Array or Arrays inside an Array is a basic and useful data structure in PHP. This post is regarding an example which describe you how to declare a multi dimensional array, how to insert data to it, how to print back those elements and how to manipulate those datum.

In this example population amounts in America is categorized into states and to each city. We have to organize them in an array, Print back them and find totals under each states and country.


<?php 
//Multi Dimensional Array
    $population=array(
                            'NY'=>array('New York'=>8008278),
                            'CA'=>array('Los Angeles'=>3694420,'San Diego'=>1223400),
                            'IL'=>array('Chicago'=>2896016),
                            'TX'=>array('Houstan'=>1953631,'Dallas'=>1188580,'San Antonio'=>1144646),
                            'AZ'=>array('Phoenix'=>1321045)
                          
                        );
                  
    $totalStatePopulation=0;                  
    $totalPoupulation=0;
   
    echo('<table border="1" width="400px">');
//$population refers to the above array $state refers to its keys(NY,CA,IL,TX,AZ) $city refers to its values(array('New York'=>8008278),array('Los Angeles'=>3694420,'San Diego'=>1223400),array('Chicago'=>2896016),array('Houstan'=>1953631,'Dallas'=>1188580,'San Antonio'=>1144646),array('Phoenix'=>1321045))
    foreach($population as $state=>$city){
        $totalStatePopulation=0;
      
         //$city is the same variable in the outer loop. So, $name refers to array $city 's keys. $amount is the population amount under each key.
            foreach($city as $name=>$amount){
            echo('<tr>');
//print the keys of the inner arrays
            echo('<td>'.$name.'</td>');
//prints the amounts under inner keys
            echo('<td>'.$amount.'</td>');
            echo('</tr>');
//calculate the state population of the country
            $totalStatePopulation=$totalStatePopulation+$amount;
//calculate the total population of the country
            $totalPoupulation=$totalPoupulation+$amount;
            }
          
          
            echo('<tr>');
//prints the keys of the outer array
            echo('<td>'.$state.'</td>');
            echo('<td style="color:green;font-weight:bold">'.$totalStatePopulation.'</td>');
            echo('</tr>');
    }
   
            echo('<td>Total Population</td>');
            echo('<td style="color:red;font-weight:bold">'.$totalPoupulation.'</td>');
    echo('</table>');


?>

Wednesday, May 15, 2013

How to submit data with Ajax

Hi ! This post is about a way that you can submit (to a PHP Script) a collection of data which have been collected by a HTML form . Here we are going to do it with Ajax technology. Even though this is a simple post you are required to have a somewhat knowledge in Java Script and PHP for a better understanding.
  1. Visit the JQuery site and download the JQuery API. ( Direct link to the API. Just save the page as .js)
  2. Link the JQuery API with your web page where the form is located.
  3. Write a Java Script function to collect data from the form by the IDs of the form items and send them to the PHP script. 

Form.php

<html>
<head>
    <title></title>
    <script type="text/javascript" src="jScript/jquery-1.9.1.min.js"></script> <!-- Link to the JQuery API-->
    <script>
    <!--JavaScript Function-->
    function formSubmit(){

       $.ajax({ // create an AJAX call...
        type: "POST",  // Submitting Method is POST
    url: "collectData.php", // To where the data should be submitted 
    data: { firstName: $('#fName').val(),    //Value in the textbox which is identified by ID 'fName' will be transmitted to variable firstName in the PHP script
            lastName: $('#lName').val(), //Value in the textbox which is identified by ID 'lName' will be transmitted to variable lastName in the PHP script
            age: $('#age').val(), // Value in the textbox which is identified by ID 'age' will be transmitted to variable age in the PHP script           
        },
        success: function(response) { // on success..
                alert(response);   // parameter response retrives any data which are echo by the PHP script
        }
    });
    return false;
    }
    </script>
   
</head>
    <body>
            <form onsubmit="return formSubmit()">  <!-- formSubmit() will be called at the event of click on the submit button of the particular form -->   
                <table>
                    <tr>
                    <td>First Name :</td>
                    <td><input type="text" id="fName"></td>
                    </tr>
                    <tr>
                    <td>Last Name :</td>
                    <td><input type="text" id="lName"></td>
                    </tr>
                    <tr>
                    <td>Age :</td>
                    <td><input type="text" id="age"></td>
                    </tr>
                    <tr>
                    <td></td>
                    <td><input type="submit" value="Submit"></td>
                    </tr>
                </table>
            </form>
    </body>
</html>




collectData.php

<?php   

$firstName=$_POST['firstName']; //Catch the data from form.php and store it in the particular variable
$lastName=$_POST['lastName'];
$age=$_POST['age'];

//Now we have stored the required data in the PHP script. Anything can be done on that data with PHP.


echo("Success"); // This statement will be returned to the parameter response.

?>



With this method you will not be re-directed to the PHP file as the traditional way. Have fun !!!!!