Thursday, March 15, 2018

Sending email using PHP

To send email we need
1. The mail server
2. User ID
3. Password
4. From with syntax as SenderName <emailid>
5. To
6. Subject
7. body

Example
1. mail server : mail.impeccableitacademy.com
2. email id : demo@impeccableitacademy.com
3. password : Yvm7*2l8
4. From : Impeccable IT Academy<info@impeccableitacademy.com>
5. To : bpsharma@gmail.com
6. subject: Job Fair at RDEC
7. body : Dear all, you all are invited for job fair. Dr B P Sharma

PHP code: sendemail.php

<?php
 require_once "Mail.php";
 $from = "Impeccable IT Academy<info@impeccableitacademy.com>";
 $to = "bpsharma@gmail.com";
 $subject = "Job Fair at RDEC";
 $body = "Dear all, you all are invited for job fair. Dr B P Sharma";
 $host = "mail.impeccableitacademy.com";
 $username = "demo@impeccableitacademy.com";
 $password = "Yvm7*2l8";

 $headers = array ('From' => $from,'To' => $to,'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,'auth' => true,'username' => $username,'password' => $password));
 $mail = $smtp->send($to, $headers, $body);
 if (PEAR::isError($mail))
    echo("<p>".$mail->getMessage()."</p>");
 else
   echo("<p>Message successfully sent!</p>");
 ?>


Saving data in database using PDO

Instructions

Sample database: demo
Sample table: persons
Columns:
id - int
first_name - vc-50
last_name - vc - 50
email - vc - 50

PHP code and HTML code : addperson.php

<?php
$message="";
if(isset($_POST['id']))
{
try
{
extract($_POST); //To automatically create the variables with field name
$sql="INSERT INTO persons VALUES($id,'$first_name','$last_name','$email')";
$pdo = new PDO("mysql:host=localhost;dbname=demo", "root", "");
$pdo->query($sql);
unset($pdo);
$message="Record Saved";
}
catch(PDOException $ex)
{
$message="Error : ".$ex->getMessage();
}
}
?>
<form action="addperson.php" method="post">
Person ID <input name="id" type="text" required="required"/><br />
First Name <input name="first_name" type="text" required="required"/><br />
Last Name <input name="last_name" type="text" required="required"/><br />
Email <input name="email" type="text" required="required"/><br />
<input name="submit" type="submit" value="Save Person" />
</form>
<div><?php echo $message; ?></div>



Wednesday, March 7, 2018

Using AngularJS & JSON with PHP & MySQL

Test Case

- Read all Students and their city names using PHP and MySQL and convert into JSON
- Show all the student names and their city names on the webpage using AngujarJS
- When we type name of a student or city name filter the records

PHP & MySQL code

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="namesCtrl">
<input type="text" ng-model="name">
<ul>
<span ng-repeat="x in names | filter:name">
<div>
Name {{ x.name}}
City {{x.city}}
</div>
</span>
</ul>
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
<?php
$cn=mysqli_connect("localhost","root","","cbp");
$sql="Select name,city from student";
$result=mysqli_query($cn,$sql);
while($r=mysqli_fetch_row($result))
{
?>
{name:'<?php echo $r[0]?>',city:'<?php echo $r[1]?>'},
<?php
}
mysqli_close($cn);
?>
];
});
</script>
</body>
</html>

Front-End using HTML, AngularJS and JSON


<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="namesCtrl">
<input type="text" ng-model="name">
<p>Looping with objects:</p>
<ul>
<span ng-repeat="x in names | filter:name">
<div>
Name {{ x.name}}
City {{x.city}}
</div>
</span>
</ul>
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
{name:'Rakesh Kumar',city:'Ghaziabad'},
{name:'Nitin Verma',city:'Delhi'},
{name:'Jatin Kumar',city:'Delhi'},
{name:'Vivek Verma',city:'Ghaziabad'},
];
});
</script>
</body>
</html>

Assignment

Create a table Item having fields
1. Itemcode,char,10, primary key
2. Itemname, varchar, 50, not null
3. Price, float, not null
4. Description, varchar, 250, not null
5. Image, varchar, 15, not null (Use the ItemCode as File Name with given extension from .jpg, .gif,.png)

Create a form input the data and save input database

Show all the items on the web page with filter